This is the mail archive of the libc-alpha@sourceware.org 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]

[patch] add a new test for the strnlen func and update a few others


just realized that the string.c test we have in uClibc is the same as glibc's 
tester.c ... a while back i hit some bugs in strnlen() dealing with very 
large values ((size_t)-1 to be exact), so i added a new test_strnlen() based 
upon the existing test_strlen() to detect the issue as well as in strncat() 
and strncmp()
-mike
Index: ChangeLog
===================================================================
RCS file: /cvs/glibc/libc/ChangeLog,v
retrieving revision 1.9988
diff -u -p -r1.9988 ChangeLog
--- ChangeLog	8 Feb 2006 18:25:19 -0000	1.9988
+++ ChangeLog	14 Feb 2006 02:23:16 -0000
@@ -1,3 +1,10 @@
+2006-02-13  Mike Frysinger  <vapier@gentoo.org>
+
+	* string/tester.c (test_strnlen): New test.
+	(test_strncat): Test lengths where the sign bit is set.
+	(test_strncmp): Likewise.
+	* string/tester.c (test_strnlen): New test.
+
 2006-02-08  Ulrich Drepper  <drepper@redhat.com>
 
 	* sysdeps/unix/sysv/linux/bits/sched.h: Declare unshare.
Index: string/tester.c
===================================================================
RCS file: /cvs/glibc/libc/string/tester.c,v
retrieving revision 1.46
diff -u -p -r1.46 tester.c
--- string/tester.c	6 Nov 2005 02:05:17 -0000	1.46
+++ string/tester.c	14 Feb 2006 02:23:16 -0000
@@ -344,6 +344,9 @@ test_strncat (void)
 
   (void) strncat (one, "gh", 2);
   equal (one, "abcdgh", 12);		/* Count and length equal. */
+
+  (void) strncat (one, "ij", (size_t)-1);	/* set sign bit in count */
+  equal (one, "abcdghij", 13);
 }
 
 static void
@@ -364,6 +367,8 @@ test_strncmp (void)
   check (strncmp ("abce", "abc", 3) == 0, 11);	/* Count == length. */
   check (strncmp ("abcd", "abce", 4) < 0, 12);	/* Nudging limit. */
   check (strncmp ("abc", "def", 0) == 0, 13);	/* Zero count. */
+  check (strncmp ("abc", "", (size_t)-1) > 0, 14);	/* set sign bit in count */
+  check (strncmp ("abc", "abc", (size_t)-2) == 0, 15);
 }
 
 static void
@@ -430,6 +435,29 @@ test_strlen (void)
 }
 
 static void
+test_strnlen (void)
+{
+  it = "strnlen";
+  check (strnlen ("", 10) == 0, 1);		/* Empty. */
+  check (strnlen ("a", 10) == 1, 2);		/* Single char. */
+  check (strnlen ("abcd", 10) == 4, 3);		/* Multiple chars. */
+  check (strnlen ("foo", (size_t)-1) == 3, 4);	/* limits of n. */
+
+  {
+    char buf[4096];
+    int i;
+    char *p;
+    for (i=0; i < 0x100; i++)
+      {
+	p = (char *) ((unsigned long int)(buf + 0xff) & ~0xff) + i;
+	strcpy (p, "OK");
+	strcpy (p+3, "BAD/WRONG");
+	check (strnlen (p, 100) == 2, 5+i);
+      }
+   }
+}
+
+static void
 test_strchr (void)
 {
   it = "strchr";
@@ -1382,6 +1410,9 @@ main (void)
   /* strlen.  */
   test_strlen ();
 
+  /* strnlen.  */
+  test_strnlen ();
+
   /* strchr.  */
   test_strchr ();
 

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