Re: [PATCH] C undefined behavior fix

From: Paul Koning (
Date: Wed Jan 02 2002 - 11:45:37 EST

>>>>> "Jakub" == Jakub Jelinek <> writes:

 Jakub> On Wed, Jan 02, 2002 at 08:39:20AM -0700, Tom Rini wrote:
>> Well, Paulus wrote 'strcpy' not 'memcpy', so why does gcc get to
>> assume it's safe to change it? In this case it's certainly not.

 Jakub> But unless you trigger undefined behaviour, strcpy(x, "foobar"
 Jakub> + n) is equal to memcpy(x, "foobar" + n, sizeof("foobar") -
 Jakub> n); and the latter is more efficient (you don't have to check
 Jakub> for end-of-string during copying).

I'd say it's the same even if you DO trigger undefined behavior.

"Undefined" means "anything can happen, don't blame the compiler". In
particular, different things may happen at different times, under
different compilers, under different phases of the moon, etc...

The change from strcpy to memcpy either replaces a defined action by
the equivalent one, or replaces an undefined action by an undefined
action. In the undefined case, the outcome may be different after the
change, but that's perfectly ok, that's what "undefined" means.

It might be interesting for the compiler to warn about this coding
error (since it presumably can detect it). But "fixing" the behavior
of undefined code seems like a strange thing to do.


To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

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