public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/51568] New: Enum value is not extracted properly via a union
@ 2011-12-15 15:57 eugene at hutorny dot in.ua
2011-12-15 17:28 ` [Bug c/51568] " rearnsha at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: eugene at hutorny dot in.ua @ 2011-12-15 15:57 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51568
Bug #: 51568
Summary: Enum value is not extracted properly via a union
Classification: Unclassified
Product: gcc
Version: 4.3.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: eugene@hutorny.in.ua
Target: ARM
Created attachment 26103
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26103
Test example
A wrong value is received when a value is assigned via union and read its
member.
Consider the following data structures:
enum en { v0, v1, v2, v3, v4, v5, v6, v7 };
struct rec {
unsigned short w;
unsigned char c;
enum en e;
} __attribute__((__packed__));
union un {
struct rec r;
unsigned v;
} __attribute__((__packed__));
void pass(unsigned v) {
union un u;
u.v = v;
printf("r.w=%X r.c=%d r.e=%d\n", u.r.w, u.r.c, u.r.e );
}
int main(int argc, char** argv) {
union un u;
u.r.c = '8';
u.r.e = v2;
u.r.w = 0xC5FF;
printf("r.w=%X r.c=%d r.e=%d\n", u.r.w, u.r.c, u.r.e );
pass(u.v);
return 0;
}
Sample above compiled with gcc (Debian 4.3.2-1.1) 4.3.2 prints the following:
r.w=C5FF r.c=56 r.e=2
r.w=C5FF r.c=56 r.e=34050
When compiled with arm-linux-gnueabi-gcc (Debian 4.3.2-1.1) 4.3.2 prints:
r.w=C5FF r.c=56 r.e=2
r.w=C5FF r.c=56 r.e=50434
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/51568] Enum value is not extracted properly via a union
2011-12-15 15:57 [Bug c/51568] New: Enum value is not extracted properly via a union eugene at hutorny dot in.ua
@ 2011-12-15 17:28 ` rearnsha at gcc dot gnu.org
2011-12-15 17:31 ` rearnsha at gcc dot gnu.org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: rearnsha at gcc dot gnu.org @ 2011-12-15 17:28 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51568
Richard Earnshaw <rearnsha at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
--- Comment #1 from Richard Earnshaw <rearnsha at gcc dot gnu.org> 2011-12-15 17:17:34 UTC ---
On linux sizeof (struct rec) is 7, so how do you expect an unsigned (size = 4)
to hold the entire value?
If you want a packed enum, you need to specify the packed on the element
declaration, not just on the overall structure:
struct rec {
unsigned short w;
unsigned char c;
enum en e __attribute__((packed));
} __attribute__((packed));
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/51568] Enum value is not extracted properly via a union
2011-12-15 15:57 [Bug c/51568] New: Enum value is not extracted properly via a union eugene at hutorny dot in.ua
2011-12-15 17:28 ` [Bug c/51568] " rearnsha at gcc dot gnu.org
@ 2011-12-15 17:31 ` rearnsha at gcc dot gnu.org
2011-12-15 20:07 ` eugene at hutorny dot in.ua
2011-12-16 11:09 ` eugene at hutorny dot in.ua
3 siblings, 0 replies; 5+ messages in thread
From: rearnsha at gcc dot gnu.org @ 2011-12-15 17:31 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51568
--- Comment #2 from Richard Earnshaw <rearnsha at gcc dot gnu.org> 2011-12-15 17:27:19 UTC ---
(In reply to comment #1)
> On linux sizeof (struct rec) is 7, so how do you expect an unsigned (size = 4)
> to hold the entire value?
>
> If you want a packed enum, you need to specify the packed on the element
> declaration, not just on the overall structure:
>
> struct rec {
> unsigned short w;
> unsigned char c;
> enum en e __attribute__((packed));
> } __attribute__((packed));
Actually that doesn't work. The only ways to pack the enum into 8 bits are to
either:
1) Use -fshort-enums (ABI change)
2) Use a bitfield expression in your struct.
struct rec {
unsigned short w;
unsigned char c;
enum en e:8;
} __attribute__((packed));
Sorry for the confusion.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/51568] Enum value is not extracted properly via a union
2011-12-15 15:57 [Bug c/51568] New: Enum value is not extracted properly via a union eugene at hutorny dot in.ua
2011-12-15 17:28 ` [Bug c/51568] " rearnsha at gcc dot gnu.org
2011-12-15 17:31 ` rearnsha at gcc dot gnu.org
@ 2011-12-15 20:07 ` eugene at hutorny dot in.ua
2011-12-16 11:09 ` eugene at hutorny dot in.ua
3 siblings, 0 replies; 5+ messages in thread
From: eugene at hutorny dot in.ua @ 2011-12-15 20:07 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51568
--- Comment #3 from Eugene <eugene at hutorny dot in.ua> 2011-12-15 19:44:34 UTC ---
>On linux sizeof (struct rec) is 7, so how do you expect an unsigned (size = 4)
to hold the entire value?
Agree, that was my mistake.
I misexpected the enum to fit one byte.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/51568] Enum value is not extracted properly via a union
2011-12-15 15:57 [Bug c/51568] New: Enum value is not extracted properly via a union eugene at hutorny dot in.ua
` (2 preceding siblings ...)
2011-12-15 20:07 ` eugene at hutorny dot in.ua
@ 2011-12-16 11:09 ` eugene at hutorny dot in.ua
3 siblings, 0 replies; 5+ messages in thread
From: eugene at hutorny dot in.ua @ 2011-12-16 11:09 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51568
--- Comment #4 from Eugene <eugene at hutorny dot in.ua> 2011-12-16 10:48:59 UTC ---
Enum can be packed to one byte with __attribute__((__packed__)) :
enum en { v0, v1, v2, v3, v4, v5, v6, v7 } __attribute__((__packed__));
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-12-16 10:49 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-15 15:57 [Bug c/51568] New: Enum value is not extracted properly via a union eugene at hutorny dot in.ua
2011-12-15 17:28 ` [Bug c/51568] " rearnsha at gcc dot gnu.org
2011-12-15 17:31 ` rearnsha at gcc dot gnu.org
2011-12-15 20:07 ` eugene at hutorny dot in.ua
2011-12-16 11:09 ` eugene at hutorny dot in.ua
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).