This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] C undefined behavior fix


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]