about summary refs log tree commit diff
path: root/libio
diff options
context:
space:
mode:
authorFrédéric Bérat <fberat@redhat.com>2024-08-13 12:01:26 +0200
committerFrédéric Bérat <fberat@redhat.com>2024-08-14 11:48:34 +0200
commit3f54e459a633b4247be91b9d0f68a7e08720b8d8 (patch)
tree49e2d8a7edf5b4e24025ab0c8d0d32c411090ac0 /libio
parent2be0572f3a41d5d5a8bb3b2b04244b7c01ac0f58 (diff)
downloadglibc-3f54e459a633b4247be91b9d0f68a7e08720b8d8.tar.gz
glibc-3f54e459a633b4247be91b9d0f68a7e08720b8d8.tar.xz
glibc-3f54e459a633b4247be91b9d0f68a7e08720b8d8.zip
libio/tst-getdelim: Add new test covering NUL as a delimiter
Add a new test to getdelim to verify that '\0' can be set as a
delimiter.

Reviewed-by: Florian Weimer <fweimer@redhat.com>
Diffstat (limited to 'libio')
-rw-r--r--libio/tst-getdelim.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/libio/tst-getdelim.c b/libio/tst-getdelim.c
index e6dd964b49..db15bf9285 100644
--- a/libio/tst-getdelim.c
+++ b/libio/tst-getdelim.c
@@ -1,4 +1,6 @@
-/* Check that getdelim sets error indicator on error (BZ #29917)
+/* Test getdelim conforming to POSIX specifications.
+
+   Note: Most getdelim use cases are covered by stdio-common/tst-getline.
 
    Copyright (C) 2023-2024 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
@@ -18,18 +20,36 @@
    <https://www.gnu.org/licenses/>.  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <errno.h>
 
 #include <support/check.h>
+#include <support/support.h>
+#include <support/test-driver.h>
 
 static int
 do_test (void)
 {
+  /* Check that getdelim sets error indicator on error (BZ #29917) */
   clearerr (stdin);
   TEST_VERIFY (getdelim (0, 0, '\n', stdin) == -1);
   TEST_VERIFY (ferror (stdin) != 0);
   TEST_VERIFY (errno == EINVAL);
 
+  /* Test getdelim with NUL as delimiter */
+  verbose_printf ("Testing NUL delimiter\n");
+  char *lineptr = NULL;
+  size_t linelen = 0;
+  char membuf[] = "abc\0d\nef\0";
+  FILE *memstream = fmemopen (membuf, sizeof (membuf), "r");
+  TEST_VERIFY_EXIT (memstream != NULL);
+  TEST_VERIFY (getdelim (&lineptr, &linelen, '\0', memstream) != -1);
+  TEST_COMPARE_BLOB (lineptr, 4, "abc\0", 4);
+  TEST_VERIFY (getdelim (&lineptr, &linelen, '\0', memstream) != -1);
+  TEST_COMPARE_BLOB (lineptr, 5, "d\nef\0", 5);
+  fclose (memstream);
+  free (lineptr);
+
   return 0;
 }