This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Ensure zero termination in string/test-s*.c (was Re: segfault in test-strcmp)


On Fri, Nov 08, 2002 at 02:42:43PM -0800, Roland McGrath wrote:
> > strncpy, strncmp, strncat take a source that is described in C99 as an
> > "array", not a string, so need not be terminated.  (Though in the absence
> > of a null character strncmp might compare up to n characters even though
> > the first difference is much earlier.)
> 
> Indeed so, and since this is purpose of strn* interfaces it seems wise to
> have the testers cover the unterminated permutations as well.  The problems
> that arose with the new test programs so far were in the testers for
> functions that require strings (strchr, strcmp).

Thanks, Joseph.
The following patch fixes test-strpbrk/test-strspn/test-strncmp so that it
doesn't rely on buf1/buf2 being zero initialized and fixes
stupid_{strncmp,strncpy,stpncpy} to use strnlen, so it never looks at more
than n characters.
I'll tweak the do_random_tests routines to actually test
non-terminated strings if src + n is before or on unmapped page boundary
later.

2002-10-09  Jakub Jelinek  <jakub@redhat.com>

	* string/test-strspn.c (do_test): Ensure zero termination.
	* string/test-strpbrk.c (do_test): Likewise.
	* string/test-strncmp.c (stupid_strncmp): Use strnlen, not strlen.
	* string/test-strncpy.c (stupid_strncpy): Likewise.
	* string/test-stpncpy.c (stupid_stpncpy): Likewise.

2002-10-08  Roland McGrath  <roland@redhat.com>

	* string/test-string.h (test_init): Fill buf1 and buf2 with
	pattern.

--- libc/string/test-strspn.c.jj	2002-11-09 00:25:40.000000000 +0100
+++ libc/string/test-strspn.c	2002-11-09 00:53:49.000000000 +0100
@@ -99,7 +99,7 @@ do_test (size_t align, size_t pos, size_
   char *acc, *s;
 
   align &= 7;
-  if (align + pos >= page_size || len > 240 || ! len)
+  if (align + pos + 10 >= page_size || len > 240 || ! len)
     return;
 
   acc = buf2 + (random () & 255);
@@ -120,6 +120,12 @@ do_test (size_t align, size_t pos, size_
   s[pos] = random () & 255;
   if (strchr (acc, s[pos]))
     s[pos] = '\0';
+  else
+    {
+      for (i = pos + 1; i < pos + 10; ++i)
+	s[i] = random () & 255;
+      s[i] = '\0';
+    }
 
   if (HP_TIMING_AVAIL)
     printf ("Length %4zd, alignment %2zd, acc len %2zd:", pos, align, len);
--- libc/string/test-string.h.jj	2002-11-09 00:25:39.000000000 +0100
+++ libc/string/test-string.h	2002-11-09 00:26:20.000000000 +0100
@@ -143,6 +143,9 @@ test_init (void)
       printf ("Setting seed to 0x%x\n", seed);
       srandom (seed);
     }
+
+  memset (buf1, 0xa5, page_size);
+  memset (buf2, 0x5a, page_size);
 }
 
 #endif
--- libc/string/test-strpbrk.c.jj	2002-11-09 00:33:09.000000000 +0100
+++ libc/string/test-strpbrk.c	2002-11-09 00:53:37.000000000 +0100
@@ -97,7 +97,7 @@ do_test (size_t align, size_t pos, size_
   char *rej, *s;
 
   align &= 7;
-  if (align + pos >= page_size || len > 240)
+  if (align + pos + 10 >= page_size || len > 240)
     return;
 
   rej = buf2 + (random () & 255);
@@ -127,6 +127,12 @@ do_test (size_t align, size_t pos, size_
 	}
     }
   s[pos] = rej[random () % (len + 1)];
+  if (s[pos])
+    {
+      for (i = pos + 1; i < pos + 10; ++i)
+	s[i] = random () & 255;
+      s[i] = '\0';
+    }
   result = STRPBRK_RESULT (s, pos);
 
   if (HP_TIMING_AVAIL)
--- libc/string/test-strncmp.c.jj	2002-11-09 00:25:39.000000000 +0100
+++ libc/string/test-strncmp.c	2002-11-09 00:46:09.000000000 +0100
@@ -42,7 +42,7 @@ simple_strncmp (const char *s1, const ch
 int
 stupid_strncmp (const char *s1, const char *s2, size_t n)
 {
-  size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
+  size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1;
   int ret = 0;
 
   n = ns1 < n ? ns1 : n;
--- libc/string/test-strncpy.c.jj	2002-11-09 00:25:39.000000000 +0100
+++ libc/string/test-strncpy.c	2002-11-09 00:48:23.000000000 +0100
@@ -47,8 +47,8 @@ simple_strncpy (char *dst, const char *s
 char *
 stupid_strncpy (char *dst, const char *src, size_t n)
 {
-  size_t ns = strlen (src);
-  size_t i, nc = n < ns ? n : ns;
+  size_t nc = strnlen (src, n);
+  size_t i;
 
   for (i = 0; i < nc; ++i)
     dst[i] = src[i];
--- libc/string/test-stpncpy.c.jj	2002-11-09 00:25:39.000000000 +0100
+++ libc/string/test-stpncpy.c	2002-11-09 00:48:53.000000000 +0100
@@ -47,8 +47,8 @@ simple_stpncpy (char *dst, const char *s
 char *
 stupid_stpncpy (char *dst, const char *src, size_t n)
 {
-  size_t ns = strlen (src);
-  size_t i, nc = n < ns ? n : ns;
+  size_t nc = strnlen (src, n);
+  size_t i;
 
   for (i = 0; i < nc; ++i)
     dst[i] = src[i];


	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]