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]

i386 optimized string functions incorrectly treat size_t argument as ssize_t


three of the i386 optimized asm functions (strncpy, strncat, strncmp) use the 
asm instruction 'js' to test the size_t __n argument.  since size_t is 
unsigned and js tests the sign bit, passing a value like (size_t)-1 will 
cause these functions to behave incorrectly.

for example, this simple test works with the C implementation, but fails with 
the asm version:
assert (strncmp("foo", "bar", (size_t)-1) != 0);

find attached a patch which should resolve the issue by utilizing 'jz' (plus a 
trick) instead of 'js'
-mike
2005-08-31  Mike Frysinger  <vapier@gentoo.org>

	* sysdeps/i386/bits/string.h (strncpy, strncat, strncmp): use 'jz'
	instead of 'js' asm instructions to test the size_t length variable.

Index: sysdeps/i386/bits/string.h
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/i386/bits/string.h,v
retrieving revision 1.23
diff -u -p -r1.23 string.h
--- sysdeps/i386/bits/string.h
+++ sysdeps/i386/bits/string.h
@@ -385,9 +385,10 @@ strncpy (char *__dest, __const char *__s
   register unsigned long int __d0, __d1, __d2;
   __asm__ __volatile__
     ("cld\n"
+     "incl	%2\n"
      "1:\n\t"
      "decl	%2\n\t"
-     "js	2f\n\t"
+     "jz	2f\n\t"
      "lodsb\n\t"
      "stosb\n\t"
      "testb	%%al,%%al\n\t"
@@ -436,9 +437,10 @@ strncat (char *__dest, __const char *__s
      "repne; scasb\n\t"
      "decl	%1\n\t"
      "movl	%4,%2\n"
+     "incl	%2\n"
      "1:\n\t"
      "decl	%2\n\t"
-     "js	2f\n\t"
+     "jz	2f\n\t"
      "lodsb\n\t"
      "stosb\n\t"
      "testb	%%al,%%al\n\t"
@@ -496,9 +498,10 @@ strncmp (__const char *__s1, __const cha
   register int __res;
   __asm__ __volatile__
     ("cld\n"
+     "incl	%3\n"
      "1:\n\t"
      "decl	%3\n\t"
-     "js	2f\n\t"
+     "jz	2f\n\t"
      "lodsb\n\t"
      "scasb\n\t"
      "jne	3f\n\t"

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