public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/51628] New: __attribute__((packed)) is unsafe in some cases
@ 2011-12-19 23:00 Keith.S.Thompson at gmail dot com
  2011-12-19 23:17 ` [Bug c/51628] " Keith.S.Thompson at gmail dot com
                   ` (23 more replies)
  0 siblings, 24 replies; 25+ messages in thread
From: Keith.S.Thompson at gmail dot com @ 2011-12-19 23:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51628

             Bug #: 51628
           Summary: __attribute__((packed)) is unsafe in some cases
    Classification: Unclassified
           Product: gcc
           Version: 4.5.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: Keith.S.Thompson@gmail.com


Created attachment 26147
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26147
packed.c test case with output in a comment

I've seen this problem with gcc 4.5.1 on SPARC Solaris 9.  I presume it will
affect other versions of gcc on other platforms with strict alignment
requirements (unlike x86, which supports misaligned accesses in hardware as I
understand it).  I think it also applies to "#pragma pack".

The gcc extension __attribute__(packed), which applied to a struct, has the
following semantics (quoting the gcc documentation for 4.5.2):

     The `packed' attribute specifies that a variable or structure field
     should have the smallest possible alignment--one byte for a
     variable, and one bit for a field, unless you specify a larger
     value with the `aligned' attribute.

When a program accesses a misaligned member of a packed struct, the compiler
generates whatever code is necessary to read or write the correct value.

If the address of a misaligned member is stored in a pointer object,
dereferencing that pointer doesn't give the compiler an opportunity to generate
that extra code.

The attached program demonstrates the problem, and includes (as a comment) the
output I get on Ubuntu x86 (ok) and Solaris 9 SPARC (bus error).

See also
http://stackoverflow.com/questions/8568432/is-gccs-attribute-packed-pragma-pack-unsafe/

I don't believe it would be practical to fix this (though there might be some
clever solution I haven't thought of).  But at least I suggest mentioning this
issue in the documentation.


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2015-03-10 13:40 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-19 23:00 [Bug c/51628] New: __attribute__((packed)) is unsafe in some cases Keith.S.Thompson at gmail dot com
2011-12-19 23:17 ` [Bug c/51628] " Keith.S.Thompson at gmail dot com
2011-12-19 23:38 ` pinskia at gcc dot gnu.org
2011-12-20  0:43 ` Keith.S.Thompson at gmail dot com
2011-12-20 10:22 ` rguenth at gcc dot gnu.org
2011-12-20 10:54 ` ebotcazou at gcc dot gnu.org
2011-12-20 11:19 ` rguenther at suse dot de
2011-12-20 11:20 ` ebotcazou at gcc dot gnu.org
2011-12-20 11:34 ` rguenther at suse dot de
2011-12-20 11:57 ` ebotcazou at gcc dot gnu.org
2011-12-20 12:00 ` rguenther at suse dot de
2011-12-20 12:29 ` ebotcazou at gcc dot gnu.org
2011-12-20 13:11 ` jakub at gcc dot gnu.org
2011-12-20 13:29 ` rguenther at suse dot de
2011-12-20 17:45 ` ebotcazou at gcc dot gnu.org
2013-04-02  0:58 ` peter at axium dot co.nz
2013-04-02  8:30 ` rguenth at gcc dot gnu.org
2013-04-02  8:31 ` rguenth at gcc dot gnu.org
2013-04-02 20:21 ` peter at axium dot co.nz
2013-04-03  7:30 ` ebotcazou at gcc dot gnu.org
2013-04-03  7:55 ` rguenther at suse dot de
2013-04-03  8:51 ` ebotcazou at gcc dot gnu.org
2013-04-03  9:20 ` rguenther at suse dot de
2015-03-10 13:29 ` sven.koehler at gmail dot com
2015-03-10 13:40 ` sven.koehler at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).