Browse Source

include/libc-string_i386.h: fix a bug where memset('\xff') misbehaves
Rules.mak: add -funsigned-char, to forestall future PITA

Denis Vlasenko 15 years ago
parent
commit
6eb4415eaa
2 changed files with 6 additions and 2 deletions
  1. 3 1
      Rules.mak
  2. 3 1
      include/libc-string_i386.h

+ 3 - 1
Rules.mak

@@ -467,9 +467,11 @@ endif
 
 NOSTDLIB_CFLAGS:=$(call check_gcc,-nostdlib,)
 # Some nice CFLAGS to work with
+# Why -funsigned-char: I hunted a bug related to incorrect
+# sign extension of 'char' type for 10 hours straight. Not fun.
 CFLAGS := -include $(top_builddir)include/libc-symbols.h \
 	$(XWARNINGS) $(CPU_CFLAGS) $(SSP_CFLAGS) \
-	-fno-builtin -nostdinc -I$(top_builddir)include -I. \
+	-funsigned-char -fno-builtin -nostdinc -I$(top_builddir)include -I. \
 	-I$(top_srcdir)libc/sysdeps/linux/$(TARGET_ARCH)
 
 # Make sure that we can be built with non-C99 compilers, too.

+ 3 - 1
include/libc-string_i386.h

@@ -26,7 +26,9 @@ void *inlined_memset_const_c_count4(void *s, unsigned eax, unsigned count)
 		return s;
 	}
 
-	eax *= 0x01010101; /* done at compile time */
+	/* You wonder why & 0xff is needed? Try memset(p, '\xff', size).
+	 * If char is signed, '\xff' == -1! */
+	eax = (eax & 0xff) * 0x01010101; /* done at compile time */
 
 	if (count == 2) {
 		*(short *)(s + 0) = eax;