[PATCH] C undefined behavior fix

Momchil Velikov
2002-01-01T23:03:39Z

[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.

Thus
strcpy (dst, "abcdef" + 2)
gives
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);

Regards,
-velco

--- 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);