[PATCH] libio: Add terminating NUL when the first character is EOF in getdelim [BZ #28038]

Eric Blake eblake@redhat.com
Thu Nov 13 21:39:17 GMT 2025


On Thu, Nov 13, 2025 at 01:23:04PM -0800, Collin Funk wrote:
> > I don't have any strong opinions as to which course glibc should take;
> > but I do think that gnulib should match whatever behavior glibc
> > decides on, since we have now got a good list of multiple other libcs
> > that are split on which behavior makes more sense.
> 
> Thanks! I'll work on a patch that reverts the previous change and
> documents the behavior. The subsequent Gnulib patch should be simple as
> well. It is just adjusting the tests that I added for this to expect the
> opposite.

One thing to consider: you could patch getdelim() to guarantee that if
*lineptr is NULL on entry, then it writes *lineptr[0] to 0 after
allocation and before reading from the buffer; and also guarantee that
if it fails with errno set after a partial read that it writes a NUL
at the point where read stopped.  That way, the only time *lineptr
would NOT be NUL-terminated would be if the user passed in a
pre-existing buffer that was not NUL-terminated; in all other cases,
leaving the buffer unchanged on error or EOF means you still have tne
NUL in place from entry.  That would at least get rid of the UB
pointed out in the bug where the newly-malloc'd buffer is completely
uninitialized.  That's different than the current patch under question
which merely writes *lineptr[0] on encountering EOF; and my
alternative patch idea will not protect users that pass in their own
pre-malloc'd uninitialized buffer, but might be a better compromise
position compared to a full reversion.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org



More information about the Libc-alpha mailing list