diff mbox series

[v2] libio/tst-getdelim: Add new test covering NUL as a delimiter

Message ID 20240814085259.453799-1-fberat@redhat.com
State New
Headers show
Series [v2] libio/tst-getdelim: Add new test covering NUL as a delimiter | expand

Commit Message

Frederic Berat Aug. 14, 2024, 8:52 a.m. UTC
Hello,

This patch adds new tests to tst-getdelim to cover the case where '\0'
is set as delimiter.

This should cover the missing test not covered by tst-getline available
for review:
https://inbox.sourceware.org/libc-alpha/4c39a1af-f4c4-683d-13d4-88c3abe8e3@redhat.com/T/#t

I'm skipping swbz#28038 based on the discussion around getline.

Fred.

--8<--
Subject: [PATCH] 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.
---
 libio/tst-getdelim.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

Comments

Florian Weimer Aug. 14, 2024, 9:23 a.m. UTC | #1
* Frédéric Bérat:

> diff --git a/libio/tst-getdelim.c b/libio/tst-getdelim.c
> index e6dd964b49..709996c204 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;
>  }

This version looks okay to me.

Reviewed-by: Florian Weimer <fweimer@redhat.com>

Thanks,
Florian
Frederic Berat Aug. 14, 2024, 9:50 a.m. UTC | #2
On Wed, Aug 14, 2024 at 11:23 AM Florian Weimer <fweimer@redhat.com> wrote:

> * Frédéric Bérat:
>
> > diff --git a/libio/tst-getdelim.c b/libio/tst-getdelim.c
> > index e6dd964b49..709996c204 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;
> >  }
>
> This version looks okay to me.
>
> Reviewed-by: Florian Weimer <fweimer@redhat.com>
>

Thanks, pushed to the repository.


>
> Thanks,
> Florian
>
>
diff mbox series

Patch

diff --git a/libio/tst-getdelim.c b/libio/tst-getdelim.c
index e6dd964b49..709996c204 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;
 }