Re: [PATCH] C undefined behavior fix

Paul Koning (pkoning@equallogic.com)
Wed, 2 Jan 2002 11:45:37 -0500 (EST)


>>>>> "Jakub" == Jakub Jelinek <jakub@redhat.com> 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.

paul

-
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/