[PATCH] C undefined behavior fix
Alexandre Oliva
aoliva@redhat.com
Sun Jan 6 13:34:00 GMT 2002
On Jan 5, 2002, Linus Torvalds <torvalds@transmeta.com> wrote:
> I think what people object to is optimizations that cannot be reasonably
> explained in any logical manner.
Err.. What's illogical in the following definition:
An expression such as:
strcpy (to, "string literal" + offset)
where `"string literal" + offset' stands for an expression that can be
determined, at compile time, to be a pointer to the beginning of a
string literal not containing NULs, plus a (possibly negative) offset
that cannot be computed at compile time, is replaced with:
memcpy (to, "string literal" + offset, sizeof "string literal" - offset)
Note: it is not necessary to verify that `offset' is in the interval
0..strlen("string literal"), because if it is not, undefined behavior
is invoked, since strcpy would dereference a pointer to memory outside
the object that was originally pointed to.
--
Alexandre Oliva Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist *Please* write to mailing lists, not to me
More information about the Gcc
mailing list