Summary: | escaping address of packed field should trigger warning | ||
---|---|---|---|
Product: | gcc | Reporter: | Mikael Pettersson <mikpelinux> |
Component: | c | Assignee: | Not yet assigned to anyone <unassigned> |
Status: | RESOLVED FIXED | ||
Severity: | enhancement | CC: | egallager, gcc-bugs, hp, msebor, rguenth |
Priority: | P3 | Keywords: | diagnostic |
Version: | 4.5.0 | ||
Target Milestone: | --- | ||
See Also: | https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68160 | ||
Host: | Target: | ||
Build: | Known to work: | 10.1.0, 9.2.0 | |
Known to fail: | Last reconfirmed: | 2012-02-01 00:00:00 | |
Attachments: | Another testcase |
Description
Mikael Pettersson
2009-10-23 16:05:53 UTC
To whomever will fix this: beware not to introduce warnings for targets where the "packed" layout is the default. (As has happened in the past for other "packed warnings".) Confirmed. Created attachment 28481 [details]
Another testcase
Testcase which demonstrates more issues.
Gcc does warn (actually it even errs) if you try to pass unaligned variable by reference but does not do this when you use pointers: $ g++ -Wall -c pack_warns.cpp pack_warns.cpp:22:9: error: cannot bind packed field 't->Test::x' to 'float&' Also it'll fail to warn if you use #pragma pack instead of __attribute__((packed)): $ g++ -Wall -c -DUSE_PRAGMA pack_warns.cpp # Compiles wo warnings Just wanted to mention that users frequently run into errors with unaligned data (especially on targets where it really matters e.g. on ARM) so this might be important issue. *** Bug 79918 has been marked as a duplicate of this bug. *** *** Bug 54032 has been marked as a duplicate of this bug. *** Starting with version 9 GCC diagnoses both calls in the test case in comment #0 (the test case with #pragma pack in comment #3 is not diagnosed): $ cat pr41809.c && gcc -O2 -S -Wall pr41809.c void f(int *); struct s { int x; char c; } __attribute__((__packed__)); struct s A[10]; int main(void) { for (int i = 0; i != sizeof(A)/sizeof(A[0]); ++i) { f(&A[i].x); f((int*)(char*)&A[i].x); } } pr41809.c: In function ‘main’: pr41809.c:13:11: warning: taking address of packed member of ‘struct s’ may result in an unaligned pointer value [-Waddress-of-packed-member] 13 | f(&A[i].x); | ^~~~~~~ pr41809.c:14:11: warning: taking address of packed member of ‘struct s’ may result in an unaligned pointer value [-Waddress-of-packed-member] 14 | f((int*)(char*)&A[i].x); | ^~~~~~~~~~~~~~~~~~~~ |