This is the mail archive of the gcc-bugs@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: target/9080: optimisation on sparc leads to unalligned access in memcpy


Synopsis: optimisation on sparc leads to unalligned access in memcpy

Responsible-Changed-From-To: unassigned->rth
Responsible-Changed-By: rth
Responsible-Changed-When: Sun Jan 26 10:13:38 2003
Responsible-Changed-Why:
    .
State-Changed-From-To: open->closed
State-Changed-By: rth
State-Changed-When: Sun Jan 26 10:13:38 2003
State-Changed-Why:
    I'm fairly sure this has been discussed before, but I can't find
    the reference right away.  The problem is known, and I consider
    the input program incorrect.
    
    The problem is that alignof(struct iphdr) == 4, and yet the
    given pointer to that structure is only 2 byte aligned.  This
    violates section 6.3.2.3/7, to wit:
    
            If the resulting pointer is not correctly aligned for the
            pointed-to type, the behavior is undefined.
    
    GCC uses this clause to infer from the type of the pointer the
    alignment that it *must* have.
    
    There are two solutions for this piece of code.
    
    One, use packed data structures, so that they are 1 byte aligned.
    In this case something like
    
            struct iphdr_u {
              struct iphdr h __attribute__((packed));
            };
    
    followed by
            &tmp_iphdr->h.saddr
    
    Two, don't cast to struct iphdr at all.  Instead use offsetof
    to find the proper displacement into the buffer.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9080


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