[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#325670: gcc-4.0: regression: generates wrong code for inlined memcpy



On Wed, Aug 31, 2005 at 12:09:40AM +0200, Bastian Blank wrote:
> On Tue, Aug 30, 2005 at 12:34:01AM -0700, Steve Langasek wrote:
> > When passing pointers to 4-byte types to memcpy(), gcc-4.0 generates
> > wrong code which assumes that these pointers are aligned at 4-byte
> > boundaries for purposes of optimization, ignoring the implicit cast to
> > (char *) in the prototype of memcpy().

> There is no implicit cast to char *. memcpy gets two void pointers.

Hmm, yes...  Unfortunately, it appears that using (void *) is not
sufficient to stop gcc-4.0 from peeking at the pointer in this case --
whereas, if memcpy() is implemented as a function, it certainly would
be...

On Wed, Aug 31, 2005 at 12:05:35AM +0200, Bastian Blank wrote:
> On Tue, Aug 30, 2005 at 02:45:41PM -0700, Steve Langasek wrote:
> > By any chance, can you provide a reference to the C spec that shows
> > gcc's current behavior is valid?  Given that traceroute is among the
> > programs that breaks under gcc-4.0, it seems to me that the assumption
> > that it's safe to use memcpy this way has been around for quite some
> > time.

> 6.3.2.3, paragraph 7.

That paragraph appears to read:

   A pointer to an object or incomplete type may be converted to a
   pointer to a different object or incomplete type. If the resulting
   pointer is not correctly aligned for the pointed-to type, the
   behavior is undefined. Otherwise, when converted back again, the
   result shall compare equal to the original pointer. When a pointer to
   an object is converted to a pointer to a character type, the result
   points to the lowest addressed byte of the object.

So by making the initial cast from char * to the pointer type that
requires greater alignment, the programmer is invoking undefined
behavior, which means gcc's implementation of memcpy() is allowed by the
spec.  That's what I wanted to know, thanks.

-- 
Steve Langasek                   Give me a lever long enough and a Free OS
Debian Developer                   to set it on, and I can move the world.
vorlon@debian.org                                   http://www.debian.org/

Attachment: signature.asc
Description: Digital signature


Reply to: