Bug 26670 - attribute((packed)) sometimes not ignored for non-PODs
Summary: attribute((packed)) sometimes not ignored for non-PODs
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.1
: P3 normal
Target Milestone: 4.2.0
Assignee: Jason Merrill
URL:
Keywords:
Depends on: 13983 17519
Blocks:
  Show dependency treegraph
 
Reported: 2006-03-13 23:11 UTC by Debian GCC Maintainers
Modified: 2023-01-09 16:34 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.2.0
Known to fail: 4.1.1
Last reconfirmed: 2006-07-06 03:52:12


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Debian GCC Maintainers 2006-03-13 23:11:14 UTC
[forwarded from http://bugs.debian.org/356548]

Under some circumstances, non-PODs are not properly stripped of their packedness:

struct nonpod {
    nonpod();
};

struct nonpod_pack {
    nonpod n;
} __attribute__ ((packed));

struct nonpod_pack2 {
    nonpod_pack p;
} __attribute__ ((packed));

gives

foo.cc:6: warning: ignoring packed attribute on unpacked non-POD field 'nonpod nonpod_pack::n'

nonpod_pack2::p should also have the packedness ignored, since it is a non-POD.
Comment 1 Andrew Pinski 2006-03-13 23:15:08 UTC
Hmm, it does not warn about non-PODs which contain non-PODS which had been warned about already.
Comment 2 Debian GCC Maintainers 2006-03-13 23:53:42 UTC
It's not just a missing warning, it also changes behavior:

int& foo(nonpod_pack& n) {
    return n.n.i;		// this passes
}

int& foo(nonpod_pack2& p) {
    return p.p.n.i;		// this barfs
}
Comment 3 Andrew Pinski 2006-03-16 05:35:41 UTC
Hmm, related to PR 13983.
Comment 4 Andrew Pinski 2006-03-16 05:36:24 UTC
And to PR 17519.
Comment 5 Jason Merrill 2006-07-05 20:38:10 UTC
This is intended behavior: a non-POD type which has been marked as packed can be used as a packed field.  13983 and 17519 are bugs in that feature.

In this case, nonpod_pack::n is not packed, so it is safe to pack fields of nonpod_pack type.
Comment 6 Jason Merrill 2006-07-06 03:48:41 UTC
Hmm, actually comment #2 does seem like a bug, both foos should compile cleanly.
Comment 7 Jason Merrill 2006-08-28 08:31:18 UTC
Or perhaps we should ignore the __attribute__ ((packed)) on nonpod_pack, since one of its fields isn't safe to pack.  Yes, that seems like the right way to handle this testcase.
Comment 8 Jason Merrill 2006-08-29 06:55:44 UTC
Subject: Bug 26670

Author: jason
Date: Tue Aug 29 06:55:35 2006
New Revision: 116555

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116555
Log:
        PR c++/26670
        * class.c (check_field_decls): Unset TYPE_PACKED (t) if one of the
        fields can't be packed.

Added:
    trunk/gcc/testsuite/g++.dg/ext/packed11.C
Modified:
    trunk/gcc/cp/class.c

Comment 9 Jason Merrill 2006-08-29 07:10:53 UTC
Subject: Bug 26670

Author: jason
Date: Tue Aug 29 07:10:38 2006
New Revision: 116556

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116556
Log:
        PR c++/26670
        * class.c (check_field_decls): Unset TYPE_PACKED (t) if one of the
        fields can't be packed.

Modified:
    trunk/gcc/cp/ChangeLog

Comment 10 Andrew Pinski 2006-08-30 03:54:10 UTC
Fixed.
Comment 11 Debian GCC Maintainers 2006-08-30 05:26:26 UTC
reported against 4.1; bootstrapped 4.1.2 CVS with this patch with no regressions. ok to apply to the 4.1 branch?

  Matthias
Comment 12 Jason Merrill 2006-08-30 15:51:27 UTC
Subject: Bug 26670

Author: jason
Date: Wed Aug 30 15:51:17 2006
New Revision: 116591

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116591
Log:
        PR c++/26670
        * class.c (check_field_decls): Don't unset TYPE_PACKED until all
        the fields have been processed.

Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/class.c

Comment 13 Jason Merrill 2006-08-30 15:52:25 UTC
Subject: Bug 26670

Author: jason
Date: Wed Aug 30 15:52:12 2006
New Revision: 116592

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116592
Log:
        PR c++/26670
        * class.c (check_field_decls): Don't unset TYPE_PACKED until all
        the fields have been processed.

        PR c++/26670
        * class.c (check_field_decls): Unset TYPE_PACKED (t) if one of the
        fields can't be packed.

Modified:
    branches/gcc-4_1-branch/gcc/cp/ChangeLog
    branches/gcc-4_1-branch/gcc/cp/class.c

Comment 14 Jason Merrill 2006-08-30 15:52:54 UTC
fixed on 4.1 branch too.