]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
libio: Add terminating NUL when the first character is EOF in getdelim [BZ #28038]
authorCollin Funk <collin.funk1@gmail.com>
Thu, 9 Oct 2025 03:10:44 +0000 (20:10 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Fri, 10 Oct 2025 00:38:01 +0000 (17:38 -0700)
POSIX requires that the buffer used by getdelim/getline add a
terminating NUL whenever an EOF is read.

* libio/iogetdelim.c (__getdelim): Add a NUL byte when the first
__underflow is called.
* libio/tst-getdelim.c (do_test): Add a test case for the bug.

Reviewed-by: Florian Weimer <fweimer@redhat.com>
libio/iogetdelim.c
libio/tst-getdelim.c

index 0bfaef227aab0987c693b30772b847b7848f05e4..1d8975735284c6b9fbd93feb8e86bd51f674817c 100644 (file)
@@ -77,6 +77,7 @@ __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
       if (__underflow (fp) == EOF)
        {
          result = -1;
+         (*lineptr)[0] = '\0';
          goto unlock_return;
        }
       len = fp->_IO_read_end - fp->_IO_read_ptr;
index 1f35f9f685d40c20c0d39a606107687d218034c3..556697453c98a12724eb38b4ae02de4aa3d548e0 100644 (file)
@@ -27,6 +27,7 @@
 #include <support/support.h>
 #include <support/test-driver.h>
 #include <support/xstdio.h>
+#include <support/temp_file.h>
 
 static int
 do_test (void)
@@ -50,6 +51,22 @@ do_test (void)
   xfclose (memstream);
   free (lineptr);
 
+  /* Test that getdelim NUL terminates upon reading an EOF from an empty
+     file (BZ #28038).  This test fails on glibc 2.42 and earlier.  */
+  lineptr = xmalloc (1);
+  lineptr[0] = 'A';
+  linelen = 1;
+  char *file_name;
+  TEST_VERIFY_EXIT (create_temp_file ("tst-getdelim.", &file_name) != -1);
+  FILE *fp = fopen (file_name, "r");
+  TEST_VERIFY_EXIT (fp != NULL);
+  TEST_VERIFY (getdelim (&lineptr, &linelen, '\n', fp) == -1);
+  TEST_VERIFY (linelen > 0);
+  TEST_VERIFY (lineptr[0] == '\0');
+  fclose (fp);
+  free (file_name);
+  free (lineptr);
+
   return 0;
 }