[v2] test-strnlen.c: Check that strnlen won't go beyond the maximum length

Message ID CAMe9rOoZ1PtPWdFjEn0uU6BE2haG2frA=rmueYV4cOMk1hhpRw@mail.gmail.com
State Committed
Headers
Series [v2] test-strnlen.c: Check that strnlen won't go beyond the maximum length |

Commit Message

H.J. Lu March 27, 2021, 6:09 p.m. UTC
  On Sat, Mar 27, 2021 at 10:51 AM Florian Weimer <fw@deneb.enyo.de> wrote:
>
> * H. J. Lu via Libc-alpha:
>
> > Run strnlen tests on strings without the null byte around the page
> > boundary.
> > ---
> >  string/test-strnlen.c | 26 ++++++++++++++++++++++++++
> >  1 file changed, 26 insertions(+)
> >
> > diff --git a/string/test-strnlen.c b/string/test-strnlen.c
> > index 61eb521dc1..a49d93afa2 100644
> > --- a/string/test-strnlen.c
> > +++ b/string/test-strnlen.c
> > @@ -196,6 +196,31 @@ do_page_tests (void)
> >      }
> >  }
> >
> > +/* Tests meant to unveil fail on implementation that does not access bytes
> > +   without the null character around the page boundary accordingly.  */
>
> I don't understand the comment.  Based on it, I would have expected a
> null byte at the page boundary, which is incorrectly ignored by the
> implementation.

It places strings ending at page boundary without the null byte.  If an
implementation goes beyond EXP_LEN, it will trigger the segfault.

> > +static void
> > +do_page_2_tests (void)
> > +{
> > +  size_t i, exp_len, offset;
> > +  size_t last_offset = page_size / sizeof (CHAR);
> > +
> > +  CHAR *s = (CHAR *) buf2;
> > +  memset (s, 65, page_size);
>
> I think this needs to use wmemset for the WIDE case, see
> wcsmbs/test-wcsnlen.c.

Fixed.

Here is the v2 patch.
  

Comments

Florian Weimer March 27, 2021, 6:34 p.m. UTC | #1
* H. J. Lu via Libc-alpha:

> From: "H.J. Lu" <hjl.tools@gmail.com>
> Date: Sat, 27 Mar 2021 09:06:39 -0700
> Subject: [PATCH v2] test-strnlen.c: Check that strnlen won't go beyond the
>  maximum length
>
> Place strings ending at page boundary without the null byte.  If an
> implementation goes beyond EXP_LEN, it will trigger the segfault.
> ---
>  string/test-strnlen.c | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
>
> diff --git a/string/test-strnlen.c b/string/test-strnlen.c
> index 0293acbc71..d70faa26ab 100644
> --- a/string/test-strnlen.c
> +++ b/string/test-strnlen.c
> @@ -198,6 +198,35 @@ do_page_tests (void)
>      }
>  }
>  
> +/* Tests meant to unveil fail on implementations that access bytes
> +   beyond the maxium length.  */
> +
> +static void
> +do_page_2_tests (void)
> +{
> +  size_t i, exp_len, offset;
> +  size_t last_offset = page_size / sizeof (CHAR);
> +
> +  CHAR *s = (CHAR *) buf2;
> +  MEMSET (s, 65, last_offset);
> +
> +  /* Place short strings ending at page boundary without the null
> +     byte.  */
> +  offset = last_offset;
> +  for (i = 0; i < 128; i++)
> +    {
> +      /* Decrease offset to stress several sizes and alignments.  */
> +      offset--;
> +      exp_len = last_offset - offset;
> +      FOR_EACH_IMPL (impl, 0)
> +	{
> +	  /* If an implementation goes beyond EXP_LEN, it will trigger
> +	     the segfault.  */
> +	  do_one_test (impl, (CHAR *) (s + offset), exp_len, exp_len);
> +	}
> +    }
> +}

Thanks, looks good.  The comments and the commit message are much
clearer now.
  
H.J. Lu Jan. 27, 2022, 5:09 p.m. UTC | #2
On Sat, Mar 27, 2021 at 11:34 AM Florian Weimer <fw@deneb.enyo.de> wrote:
>
> * H. J. Lu via Libc-alpha:
>
> > From: "H.J. Lu" <hjl.tools@gmail.com>
> > Date: Sat, 27 Mar 2021 09:06:39 -0700
> > Subject: [PATCH v2] test-strnlen.c: Check that strnlen won't go beyond the
> >  maximum length
> >
> > Place strings ending at page boundary without the null byte.  If an
> > implementation goes beyond EXP_LEN, it will trigger the segfault.
> > ---
> >  string/test-strnlen.c | 30 ++++++++++++++++++++++++++++++
> >  1 file changed, 30 insertions(+)
> >
> > diff --git a/string/test-strnlen.c b/string/test-strnlen.c
> > index 0293acbc71..d70faa26ab 100644
> > --- a/string/test-strnlen.c
> > +++ b/string/test-strnlen.c
> > @@ -198,6 +198,35 @@ do_page_tests (void)
> >      }
> >  }
> >
> > +/* Tests meant to unveil fail on implementations that access bytes
> > +   beyond the maxium length.  */
> > +
> > +static void
> > +do_page_2_tests (void)
> > +{
> > +  size_t i, exp_len, offset;
> > +  size_t last_offset = page_size / sizeof (CHAR);
> > +
> > +  CHAR *s = (CHAR *) buf2;
> > +  MEMSET (s, 65, last_offset);
> > +
> > +  /* Place short strings ending at page boundary without the null
> > +     byte.  */
> > +  offset = last_offset;
> > +  for (i = 0; i < 128; i++)
> > +    {
> > +      /* Decrease offset to stress several sizes and alignments.  */
> > +      offset--;
> > +      exp_len = last_offset - offset;
> > +      FOR_EACH_IMPL (impl, 0)
> > +     {
> > +       /* If an implementation goes beyond EXP_LEN, it will trigger
> > +          the segfault.  */
> > +       do_one_test (impl, (CHAR *) (s + offset), exp_len, exp_len);
> > +     }
> > +    }
> > +}
>
> Thanks, looks good.  The comments and the commit message are much
> clearer now.

I am backporting this to release branches.
  

Patch

From c5d32deca0a024784fa3d9743ddffe38b567ec8c Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Sat, 27 Mar 2021 09:06:39 -0700
Subject: [PATCH v2] test-strnlen.c: Check that strnlen won't go beyond the
 maximum length

Place strings ending at page boundary without the null byte.  If an
implementation goes beyond EXP_LEN, it will trigger the segfault.
---
 string/test-strnlen.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/string/test-strnlen.c b/string/test-strnlen.c
index 0293acbc71..d70faa26ab 100644
--- a/string/test-strnlen.c
+++ b/string/test-strnlen.c
@@ -198,6 +198,35 @@  do_page_tests (void)
     }
 }
 
+/* Tests meant to unveil fail on implementations that access bytes
+   beyond the maxium length.  */
+
+static void
+do_page_2_tests (void)
+{
+  size_t i, exp_len, offset;
+  size_t last_offset = page_size / sizeof (CHAR);
+
+  CHAR *s = (CHAR *) buf2;
+  MEMSET (s, 65, last_offset);
+
+  /* Place short strings ending at page boundary without the null
+     byte.  */
+  offset = last_offset;
+  for (i = 0; i < 128; i++)
+    {
+      /* Decrease offset to stress several sizes and alignments.  */
+      offset--;
+      exp_len = last_offset - offset;
+      FOR_EACH_IMPL (impl, 0)
+	{
+	  /* If an implementation goes beyond EXP_LEN, it will trigger
+	     the segfault.  */
+	  do_one_test (impl, (CHAR *) (s + offset), exp_len, exp_len);
+	}
+    }
+}
+
 int
 test_main (void)
 {
@@ -244,6 +273,7 @@  test_main (void)
 
   do_random_tests ();
   do_page_tests ();
+  do_page_2_tests ();
   return ret;
 }
 
-- 
2.30.2