On Mon, Oct 27, 2025 at 07:38:53AM -0500, Eric Blake wrote:
> https://gitlab.com/nbdkit/nbdkit/-/commit/01b8e557ce129b
> 
> In short, while your patch changes the behavior on an empty file to
> guarantee that the buffer is NUL-terminated even though it is empty
> (on the grounds that the -1 return in THAT scenario is not an error,
> per se, because errno is not set), it ALSO has the side effect of
> changing the buffer on a non-empty file after the last line is already
> in the buffer and EOF then encountered to end the loop.  Pre-patch, it
> was possible (although questionably portable, because the POSIX
> wording is unclear) to call getline() in a loop until it returns -1,
> and then have the contents of the final line of the file (assuming the
> file was non-empty) in the buffer with no extra effort.  This works
> great for grabbing the summary line of du(1), for example.
> Post-patch, glibc now ALWAYS writes buf[0] to \0 on EOF, even if the
> file was non-empty, which breaks that convenience means of grabbing
> the last line of du.
> 
> Is there any way to refine this patch in gnulib and glibc so as to not
> break the behavior on non-empty files?  After all, if the file is
> non-empty, then by the time we encounter EOF, we are guaranteed that
> the buffer IS NUL-terminated from the previous loop.  It is only when
> the file is empty that there was no previous line, and therefore no
> guarantee of a NUL terminator in the buffer.  Would it work to change
> the behavior to add a NUL terminator at the time the buffer is first
> allocated before reading from the file, and then have EOF leave the
> buffer unchanged, instead of truncating the buffer with \0 at buf[0]
> on EOF?

So I do want to note that nbdkit _did_ have a bug here (aside from
whether or not using 'line' after EOF is correct).  That is that we
didn't check the case that 'du' produces no lines of output.  In that
case nbdkit would have segfaulted, I think, since line == NULL.

The patch above fixes both issues at the same time.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html


Reply via email to