[PATCH] Fix strnlen

Jakub Jelinek jakub@redhat.com
Mon Aug 27 13:41:00 GMT 2001


Hi!

Matt Wilson found that strnlen("foo", -1) now gives wrong answer.
Following patch should fix it, provided that we don't care about wrapping
around in address space (otherwise we'd have to decrease len and increase
ptr in each step instead of just increasing ptr).

2001-08-27  Jakub Jelinek  <jakub@redhat.com>

	* string/tst-strlen.c (main): Test strnlen (, -1) too.
	* sysdeps/generic/strnlen.c (__strnlen): Fix for maxlens with top
	bit set.

--- libc/string/tst-strlen.c.jj	Sat Jul  4 12:28:00 1998
+++ libc/string/tst-strlen.c	Mon Aug 27 22:56:58 2001
@@ -31,7 +31,8 @@ main(int argc, char *argv[])
 	      buf[words * 4 + 3] = (last & 8) != 0 ? 'e' : '\0';
 	      buf[words * 4 + 4] = '\0';
 
-	      if (strlen (buf) != words * 4 + lens[last])
+	      if (strlen (buf) != words * 4 + lens[last]
+		  || strnlen (buf, -1) != words * 4 + lens[last])
 		{
 		  printf ("failed for base=%Zu, words=%Zu, and last=%Zu\n",
 			  base, words, last);
--- libc/sysdeps/generic/strnlen.c.jj	Wed Aug 22 18:57:24 2001
+++ libc/sysdeps/generic/strnlen.c	Mon Aug 27 22:54:43 2001
@@ -36,6 +36,9 @@ __strnlen (const char *str, size_t maxle
   if (maxlen == 0)
     return 0;
 
+  if (__builtin_expect (end_ptr < str, 0))
+    end_ptr = (const char *) ~0UL;
+
   /* Handle the first few characters by reading one character at a time.
      Do this until CHAR_PTR is aligned on a longword boundary.  */
   for (char_ptr = str; ((unsigned long int) char_ptr

	Jakub



More information about the Libc-hacker mailing list