mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH] libfile: null-terminate buffer as expected
Date: Tue, 17 Sep 2019 09:49:40 +0200	[thread overview]
Message-ID: <20190917074940.fv6grmxieroachnl@pengutronix.de> (raw)
In-Reply-To: <f8e5f85c-2973-0c8a-080e-33a5254183b4@pengutronix.de>

On Mon, Sep 16, 2019 at 08:48:13PM +0200, Ahmad Fatoum wrote:
> On 9/16/19 8:38 PM, Ahmad Fatoum wrote:
> > strim calls strlen on buf internally which is undefined because buf
> > is not null-terminated. Fix this.
> 
> Please dismiss... read_file_2 has its buffer allocated with calloc,
> thus nothing to see here...

except this:

---------------------------8<-------------------------------

From b3692899dd20e85368aea114f0984cf204f24356 Mon Sep 17 00:00:00 2001
From: Sascha Hauer <s.hauer@pengutronix.de>
Date: Tue, 17 Sep 2019 09:46:46 +0200
Subject: [PATCH] libfile: Document that read_file() returns a terminated
 buffer

read_file() and read_file_2() return a '\0' terminated buffer to make
sure the buffer is usable as a string. Other code like read_file_line()
depends on this behaviour, so document it explicitly.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 lib/libfile.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/libfile.c b/lib/libfile.c
index 172602377f..648d409a01 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -158,7 +158,9 @@ EXPORT_SYMBOL_GPL(read_file_line);
  * bytes are read. The actual read size is returned in @size. -EFBIG is
  * returned if the file is bigger than @max_size, but the buffer is read
  * anyway up to @max_size in this case. Free the buffer with free() after
- * usage.
+ * usage. The allocated buffer is actually one byte bigger than the file
+ * and the extra byte is initialized to '\0' so that the returned buffer
+ * can safely be interpreted as a string.
  *
  * Return: 0 for success, or negative error code. -EFBIG is returned
  * when the file has been bigger than max_size.
-- 
2.23.0

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

      reply	other threads:[~2019-09-17  7:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-16 18:38 Ahmad Fatoum
2019-09-16 18:48 ` Ahmad Fatoum
2019-09-17  7:49   ` Sascha Hauer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190917074940.fv6grmxieroachnl@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox