Bug in MIPS strncpy
Hans-Peter Nilsson
hans-peter.nilsson@axis.com
Wed Jul 16 17:09:00 GMT 2008
> Date: Fri, 27 Jun 2008 15:47:23 -0400
> From: Jeff Johnston <jjohnstn@redhat.com>
> Index: strncpy.c
> ===================================================================
> RCS file: /cvs/src/src/newlib/libc/machine/mips/strncpy.c,v
> retrieving revision 1.1
> diff -u -p -r1.1 strncpy.c
> --- strncpy.c 11 Mar 2002 15:44:35 -0000 1.1
> +++ strncpy.c 27 Jun 2008 19:45:29 -0000
> @@ -82,6 +82,26 @@ strncpy (char *dst0, const char *src0, s
>
> dst = (unsigned char *)dst0;
> src = (unsigned const char *)src0;
> + /* Take care of any odd bytes in the source data because we
> + * want to unroll where we read ahead 2 or 4 bytes at a time and then
> + * check each byte for the null terminator. This can result in
> + * a segfault for the case where the source pointer is unaligned,
> + * the null terminator is in valid memory, but reading 2 or 4 bytes at a
> + * time blindly eventually goes outside of valid memory. */
> + while ((src & (UNROLL_FACTOR - 1)) != 0 && count > 0)
This patch broke build with trunk gcc for at least r137551,
building in a combined tree:
/home/hp/combn/mips-regobj/./gcc/xgcc -B/home/hp/combn/mips-regobj/./gcc/ -nostdinc -B/home/hp/combn/mips-regobj/mips-elf/soft-float/newlib/ -isystem /home/hp/combn/mips-regobj/mips-elf/soft-float/newlib/targ-include -isystem /home/hp/combn/combined/newlib/libc/include -B/home/hp/combn/mips-regobj/mips-elf/soft-float/libgloss/mips -L/home/hp/combn/mips-regobj/mips-elf/soft-float/libgloss/libnosys -L/home/hp/combn/combined/libgloss/mips -B/tmp/reg-mips/mips-elf/bin/ -B/tmp/reg-mips/mips-elf/lib/ -isystem /tmp/reg-mips/mips-elf/include -isystem /tmp/reg-mips/mips-elf/sys-include -L/home/hp/combn/mips-regobj/./ld -msoft-float -DPACKAGE_NAME=\"newlib\" -DPACKAGE_TARNAME=\"newlib\" -DPACKAGE_VERSION=\"1.16.0\" -DPACKAGE_STRING=\"newlib\ 1.16.0\" -DPACKAGE_BUGREPORT=\"\" -I. -I/home/hp/combn/combined/newlib/libc/machine/mips -O2 -DMISSING_SYSCALL_NAMES -fno-builtin -g -O2 -msoft-float -c -o lib_a-strncpy.o `test -f 'strncpy.c' || echo '/home/hp/combn/combined/newlib/libc/!
machine/mips/'`strncpy.c
/home/hp/combn/combined/newlib/libc/machine/mips/strncpy.c: In function 'strncpy':
/home/hp/combn/combined/newlib/libc/machine/mips/strncpy.c:91: error: invalid operands to binary & (have 'const unsigned char *' and 'int')
make[11]: *** [lib_a-strncpy.o] Error 1
make[11]: Leaving directory `/home/hp/combn/mips-regobj/mips-elf/soft-float/newlib/libc/machine/mips'
make[10]: *** [all-recursive] Error 1
(line 91 being the while-statement quoted above.)
Here's a patch that fixes the build.
Ok to apply?
2008-07-07 <hp@axis.com>
* libc/machine/mips/strncpy.c: Include stdint.h to get uintptr_t.
(strncpy): Cast src to uintptr_t before checking alignment with "&".
Index: libc/machine/mips/strncpy.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/machine/mips/strncpy.c,v
retrieving revision 1.2
diff -u -p -r1.2 strncpy.c
--- libc/machine/mips/strncpy.c 2 Jul 2008 18:17:48 -0000 1.2
+++ libc/machine/mips/strncpy.c 7 Jul 2008 13:45:29 -0000
@@ -18,6 +18,7 @@
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
+#include <stdint.h>
#if !defined(__GNUC__) || (__GNUC__ < 3)
#define __builtin_expect(a,b) a
@@ -88,7 +89,7 @@ strncpy (char *dst0, const char *src0, s
* a segfault for the case where the source pointer is unaligned,
* the null terminator is in valid memory, but reading 2 or 4 bytes at a
* time blindly eventually goes outside of valid memory. */
- while ((src & (UNROLL_FACTOR - 1)) != 0 && count > 0)
+ while (((uintptr_t) src & (UNROLL_FACTOR - 1)) != 0 && count > 0)
{
*dst++ = ch = *src++;
--count;
brgds, H-P
More information about the Newlib
mailing list