This is the mail archive of the
mailing list for the GCC project.
[PATCH] C undefined behavior fix
- From: Momchil Velikov <velco at fadata dot bg>
- To: linux-kernel at vger dot kernel dot org
- Cc: gcc at gcc dot gnu dot org, linuxppc-dev at lists dot linuxppc dot org
- Date: 02 Jan 2002 01:03:25 +0200
- Subject: [PATCH] C undefined behavior fix
[Cc: to gcc list, in case someone wants to argue about standards]
The appended patch fix incorrect code, which interferes badly with
optimizations in GCC 3.0.4 and GCC 3.1.
The GCC tries to replace the strcpy from a constant string source with
a memcpy, since the length is know at compile time.
strcpy (dst, "abcdef" + 2)
memcpy (dst, "abcdef" + 2, 5)
However, GCC does not handle the case, when the above offset (2) is
not within the bounds of the string, which result in undefined
behavior according to ANSI/ISO C99.
The error is that
strcpy (namep, "linux,phandle" + 0xc0000000);
gets emitted as
memcpy (namep, "linux,phandle" + 0xc0000000, 14 - 0xc0000000);
--- 1.3/arch/ppc/kernel/prom.c Wed Dec 26 18:27:54 2001
+++ edited/arch/ppc/kernel/prom.c Tue Jan 1 22:53:23 2002
@@ -997,7 +997,7 @@
prev_propp = &pp->next;
namep = (char *) (pp + 1);
pp->name = PTRUNRELOC(namep);
- strcpy(namep, RELOC("linux,phandle"));
+ memcpy (namep, RELOC("linux,phandle"), sizeof("linux,phandle"));
mem_start = ALIGN((unsigned long)namep + strlen(namep) + 1);
pp->value = (unsigned char *) PTRUNRELOC(&np->node);
pp->length = sizeof(np->node);