public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Warn about bitfields larger than union
@ 2011-10-30 18:12 Jon Grant
  2011-10-31 15:15 ` Kevin P. Fleming
  0 siblings, 1 reply; 4+ messages in thread
From: Jon Grant @ 2011-10-30 18:12 UTC (permalink / raw)
  To: gcc-help

Hello

I've uploaded a simple C program which shows my query:

http://pastebin.com/BQmG0m3g

Is there a way for GCC to warn when union size is exceeded by a bit-field?


Essentially a union of a type, and a bit-field. The bit-field is larger 
than the first member of the union. This means that the struct is larger 
than the uint8_t expected, it compiles as 2 bytes large. Output below.


jon@laptop:~$ gcc -Wall -o union union.c
jon@laptop:~$ ./union
sizeof(test_t): 4
t: 0x0x92f0008
reg0: 0x0x92f0008
reg1: 0x0x92f000a
sizeof(t): 4


Thank you for any feedback

Please keep my email address in any replies.

Best regards, Jon

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

* Re: Warn about bitfields larger than union
  2011-10-30 18:12 Warn about bitfields larger than union Jon Grant
@ 2011-10-31 15:15 ` Kevin P. Fleming
  2011-10-31 16:09   ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Kevin P. Fleming @ 2011-10-31 15:15 UTC (permalink / raw)
  To: gcc-help

On 10/30/2011 01:12 PM, Jon Grant wrote:
> Hello
>
> I've uploaded a simple C program which shows my query:
>
> http://pastebin.com/BQmG0m3g
>
> Is there a way for GCC to warn when union size is exceeded by a bit-field?

The 'union size' is determined from the largest member of the union.

-- 
Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
Jabber: kfleming@digium.com | SIP: kpfleming@digium.com | Skype: kpfleming
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
Check us out at www.digium.com & www.asterisk.org

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

* Re: Warn about bitfields larger than union
  2011-10-31 15:15 ` Kevin P. Fleming
@ 2011-10-31 16:09   ` Jonathan Wakely
  2011-10-31 16:10     ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2011-10-31 16:09 UTC (permalink / raw)
  To: Kevin P. Fleming; +Cc: gcc-help

On 31 October 2011 15:15, Kevin P. Fleming wrote:
> On 10/30/2011 01:12 PM, Jon Grant wrote:
>>
>> Hello
>>
>> I've uploaded a simple C program which shows my query:
>>
>> http://pastebin.com/BQmG0m3g
>>
>> Is there a way for GCC to warn when union size is exceeded by a bit-field?
>
> The 'union size' is determined from the largest member of the union.

Indeed, I don't see how the compiler could know that you didn't mean
to make the union larger than the first member.

If you want to put checks in for that, use a static assertions of some kind:

char static_assert[(sizeof(REG0) == sizeof(REG0.reg8bit))-1];

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

* Re: Warn about bitfields larger than union
  2011-10-31 16:09   ` Jonathan Wakely
@ 2011-10-31 16:10     ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2011-10-31 16:10 UTC (permalink / raw)
  To: Jon Grant; +Cc: gcc-help

Putting the OP back in the To field ...

On 31 October 2011 16:09, Jonathan Wakely wrote:
> On 31 October 2011 15:15, Kevin P. Fleming wrote:
>> On 10/30/2011 01:12 PM, Jon Grant wrote:
>>>
>>> Hello
>>>
>>> I've uploaded a simple C program which shows my query:
>>>
>>> http://pastebin.com/BQmG0m3g
>>>
>>> Is there a way for GCC to warn when union size is exceeded by a bit-field?
>>
>> The 'union size' is determined from the largest member of the union.
>
> Indeed, I don't see how the compiler could know that you didn't mean
> to make the union larger than the first member.
>
> If you want to put checks in for that, use a static assertions of some kind:
>
> char static_assert[(sizeof(REG0) == sizeof(REG0.reg8bit))-1];
>

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

end of thread, other threads:[~2011-10-31 16:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-30 18:12 Warn about bitfields larger than union Jon Grant
2011-10-31 15:15 ` Kevin P. Fleming
2011-10-31 16:09   ` Jonathan Wakely
2011-10-31 16:10     ` Jonathan Wakely

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).