public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/41809] New: escaping address of packed field should trigger warning
@ 2009-10-23 16:06 mikpe at it dot uu dot se
2009-10-26 21:47 ` [Bug other/41809] " hp at gcc dot gnu dot org
0 siblings, 1 reply; 2+ messages in thread
From: mikpe at it dot uu dot se @ 2009-10-23 16:06 UTC (permalink / raw)
To: gcc-bugs
A field in a packed struct will often not have the normal alignment for its
type. So taking the address of such a field may yield a misaligned pointer,
but gcc does not warn about that. This is similar to casting a pointer from a
less aligned type to a more aligned type, which does give a warning (with
-Wcast-align).
Example:
> cat aopf.c
#include <stdio.h>
void __attribute__((noinline)) f(int *p)
{
printf("%p\n", p);
}
struct s {
int x;
char c;
} __attribute__((__packed__));
struct s A[10];
int main(void)
{
unsigned int i;
for (i = 0; i < sizeof(A)/sizeof(A[0]); ++i) {
f(&A[i].x);
f((int*)(char*)&A[i].x);
}
return 0;
}
> gcc -Wall -Wextra -Wcast-align -O aopf.c
aopf.c: In function 'main':
aopf.c:21:4: warning: cast increases required alignment of target type
Ideally both calls to f() should trigger alignment warnings.
--
Summary: escaping address of packed field should trigger warning
Product: gcc
Version: 4.5.0
Status: UNCONFIRMED
Severity: enhancement
Priority: P3
Component: other
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: mikpe at it dot uu dot se
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41809
^ permalink raw reply [flat|nested] 2+ messages in thread
* [Bug other/41809] escaping address of packed field should trigger warning
2009-10-23 16:06 [Bug other/41809] New: escaping address of packed field should trigger warning mikpe at it dot uu dot se
@ 2009-10-26 21:47 ` hp at gcc dot gnu dot org
0 siblings, 0 replies; 2+ messages in thread
From: hp at gcc dot gnu dot org @ 2009-10-26 21:47 UTC (permalink / raw)
To: gcc-bugs
------- Comment #1 from hp at gcc dot gnu dot org 2009-10-26 21:46 -------
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".)
--
hp at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |hp at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41809
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-10-26 21:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-23 16:06 [Bug other/41809] New: escaping address of packed field should trigger warning mikpe at it dot uu dot se
2009-10-26 21:47 ` [Bug other/41809] " hp at gcc dot gnu dot org
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).