]> git.feebdaed.xyz Git - 0xmirror/glibc.git/commit
libio: null terminate the buffer upon initial allocation in getdelim
authorCollin Funk <collin.funk1@gmail.com>
Wed, 3 Dec 2025 04:02:58 +0000 (20:02 -0800)
committerCollin Funk <collin.funk1@gmail.com>
Sat, 6 Dec 2025 04:09:36 +0000 (20:09 -0800)
commit866fa41ef8521ce94ffdacfd6f1f67737899d5c9
tree39ec5e5cec048a85f59c0d0d02d3859073010275
parente2b00d59eba07a97c10d540031fedc83df8b7f76
libio: null terminate the buffer upon initial allocation in getdelim

Commit 33eff78c8b28adc4963987880e10d96761f2a167 caused issues in nbdkit
which had code similar to this to get the last line of the file:

    while (getline (&line, &len, fp) != -1)
      ;
    /* Process LINE.  */

After that commit, line[0] would be equal to '\0' instead of containing
the last line of the file like before that commit. A recent POSIX issue
clarified that the behavior before and after that commit are allowed,
since the contents of LINE are unspecified after -1 is returned
[1]. However, some programs rely on the previous behavior.

This patch null terminates the buffer upon getdelim/getline's initial
allocation. This is compatible with previous glibc versions, while also
protecting the caller from reading uninitialized memory if the file is
empty, as long as getline/getdelim does the initial allocation.

[1] https://www.austingroupbugs.net/bug_view_page.php?bug_id=1953

Suggested-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
libio/iogetdelim.c
libio/tst-getdelim.c
manual/stdio.texi