[PATCH] libbb: Fix xmalloc_fgets() newline handling

Mark Thompson mark.thompson at starleaf.com
Fri Aug 31 11:58:33 UTC 2018

Since 2da9724b56169f00bd7fb6b9a11c9409a7620981, xmalloc_fgets() will always
return the entirety of a text file rather than stopping at '\n' as one would
expect from fgets().  This is visible in for example 'head -n -1', which
outputs nothing rather than all but the last line of the input.

Signed-off-by: Mark Thompson <mark.thompson at starleaf.com>
Found from tracking down why 'ifdown foo' would always protest that 'foo' was unconfigured, despite it being in the ifstate file (but not as the first line).  The 'head -n -1' example is a much easier way to show the problem, and there are probably a few other places with unexpected behaviour because of it.

 libbb/get_line_from_file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c
index f3d6c62..ad04e3a 100644
--- a/libbb/get_line_from_file.c
+++ b/libbb/get_line_from_file.c
@@ -47,7 +47,8 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end)
 /* Get line, including trailing \n if any */
 char* FAST_FUNC xmalloc_fgets(FILE *file)
-	return bb_get_chunk_from_file(file, NULL);
+	size_t i;
+	return bb_get_chunk_from_file(file, &i);
 /* Get line.  Remove trailing \n */
 char* FAST_FUNC xmalloc_fgetline(FILE *file)

More information about the busybox mailing list