[PATCH] C undefined behavior fix

From: Momchil Velikov (velco@fadata.bg)
Date: Tue Jan 01 2002 - 18:03:25 EST


[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);
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Mon Jan 07 2002 - 21:00:15 EST