Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #23375 > unrolled thread
| Started by | John May <nospam@nospam.com> |
|---|---|
| First post | 2012-07-18 20:41 +0000 |
| Last post | 2012-07-20 07:40 +0100 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.c
Dereferencing type-punned pointer John May <nospam@nospam.com> - 2012-07-18 20:41 +0000
Re: Dereferencing type-punned pointer Keith Thompson <kst-u@mib.org> - 2012-07-18 14:14 -0700
Re: Dereferencing type-punned pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-19 01:33 +0100
Re: Dereferencing type-punned pointer Keith Thompson <kst-u@mib.org> - 2012-07-18 14:17 -0700
Re: Dereferencing type-punned pointer Ike Naar <ike@sverige.freeshell.org> - 2012-07-19 06:16 +0000
Re: Dereferencing type-punned pointer Keith Thompson <kst-u@mib.org> - 2012-07-19 01:35 -0700
Re: Dereferencing type-punned pointer Ike Naar <ike@sdf.lonestar.org> - 2012-07-19 14:11 +0000
Re: Dereferencing type-punned pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-19 01:56 +0100
Re: Dereferencing type-punned pointer Nobody <nobody@nowhere.com> - 2012-07-20 07:40 +0100
| From | John May <nospam@nospam.com> |
|---|---|
| Date | 2012-07-18 20:41 +0000 |
| Subject | Dereferencing type-punned pointer |
| Message-ID | <ju7722$ttq$2@speranza.aioe.org> |
I keep getting this warning in GCC with the code segment below. file.c:256 "warning: dereferencing type-punned pointer will break strict-aliasing rules" /*****************************************/ char buffer[SIZE]; int length, type; /* Code to fill buffer */ length = ntohs( *((unsigned short *)buffer) ); type = ntohs( *((unsigned short *)buffer + 1) ); /*****************************************/ I'm trying to take the first two bytes (in network byte order) off the buffer and put them in the length, followed by the next two bytes into the type. GCC complains about the length conversion, but not the type. What does this warning mean, and should I bother with it? I don't want to keep checking this forum, so answers by email required. Quick responses appreciated. John
[toc] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-07-18 14:14 -0700 |
| Message-ID | <lnsjcobwm2.fsf@nuthaus.mib.org> |
| In reply to | #23375 |
John May <nospam@nospam.com> writes:
> I keep getting this warning in GCC with the code segment below.
>
> file.c:256 "warning: dereferencing type-punned pointer will break
> strict-aliasing rules"
>
> /*****************************************/
> char buffer[SIZE];
> int length, type;
>
> /* Code to fill buffer */
>
> length = ntohs( *((unsigned short *)buffer) );
> type = ntohs( *((unsigned short *)buffer + 1) );
>
> /*****************************************/
>
>
> I'm trying to take the first two bytes (in network byte order) off the
> buffer and put them in the length, followed by the next two bytes into
> the type. GCC complains about the length conversion, but not the type.
>
> What does this warning mean, and should I bother with it?
Assuming buffer is aligned at an even address, buffer + 1 is at an odd
address; attempting to read a short object from an odd address can
easily crash your program. (x86 systems, I think, don't impose strict
alignment requirements.)
If you want to copy bytes from an array of char into a larger integer,
either do so explicitly or use memcpy().
You also appear to be making some assumptions about the sizes of short
and int. You should at least document those assumptions.
> I don't want to keep checking this forum, so answers by email required.
> Quick responses appreciated.
Answers posted here are not just for your benefit; they're for everyone
who reads this newsgroup. Feel free to explain why you should get
special treatment, but don't expect to convince anyone. Post here, get
your answers here.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-07-19 01:33 +0100 |
| Message-ID | <0.c048b329918d0142237a.20120719013350BST.87r4s8wpw1.fsf@bsb.me.uk> |
| In reply to | #23377 |
Keith Thompson <kst-u@mib.org> writes: > John May <nospam@nospam.com> writes: <snip> >> char buffer[SIZE]; >> int length, type; >> >> /* Code to fill buffer */ >> >> length = ntohs( *((unsigned short *)buffer) ); >> type = ntohs( *((unsigned short *)buffer + 1) ); <snip> >> I'm trying to take the first two bytes (in network byte order) off the >> buffer and put them in the length, followed by the next two bytes into >> the type. GCC complains about the length conversion, but not the type. >> >> What does this warning mean, and should I bother with it? > > Assuming buffer is aligned at an even address, buffer + 1 is at an odd > address; attempting to read a short object from an odd address can > easily crash your program. The cast has higher precedence so unless the size of short is odd, the second access uses an address with the same parity as the first. I've phrased it like this because there's nothing actually wrong with what you said -- it just isn't relevant to the code in question! <snip> -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-07-18 14:17 -0700 |
| Message-ID | <lnobncbwgy.fsf@nuthaus.mib.org> |
| In reply to | #23375 |
John May <nospam@nospam.com> writes:
[...]
> I don't want to keep checking this forum, so answers by email required.
> Quick responses appreciated.
Unless <nospam@nospam.com> is your real e-mail address, how do you
expect to get answers by e-mail?
And if it isn't, you shouldn't use a real domain name as a fake address
(yes, there really is a nospam.com); it can result in the owners of that
domain receiving spam meant for you. "example.com" is guaranteed not to
exist.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@sverige.freeshell.org> |
|---|---|
| Date | 2012-07-19 06:16 +0000 |
| Message-ID | <slrn3vfsk0f9h9.ke4.ike@sverige.freeshell.org> |
| In reply to | #23379 |
On 2012-07-18, Keith Thompson <kst-u@mib.org> wrote: > Unless <nospam@nospam.com> is your real e-mail address, how do you > expect to get answers by e-mail? > > And if it isn't, you shouldn't use a real domain name as a fake address > (yes, there really is a nospam.com); it can result in the owners of that > domain receiving spam meant for you. "example.com" is guaranteed not to > exist. Are you sure about that? "example" does not exist as a top-level domain, but is it also a reserved name for subdomains? (I thought not). Perhaps better choices would be "nospam.example" or "nospam.invalid".
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-07-19 01:35 -0700 |
| Message-ID | <lnfw8ob125.fsf@nuthaus.mib.org> |
| In reply to | #23387 |
Ike Naar <ike@sverige.freeshell.org> writes:
> On 2012-07-18, Keith Thompson <kst-u@mib.org> wrote:
>> Unless <nospam@nospam.com> is your real e-mail address, how do you
>> expect to get answers by e-mail?
>>
>> And if it isn't, you shouldn't use a real domain name as a fake address
>> (yes, there really is a nospam.com); it can result in the owners of that
>> domain receiving spam meant for you. "example.com" is guaranteed not to
>> exist.
>
> Are you sure about that? "example" does not exist as a top-level
> domain, but is it also a reserved name for subdomains? (I thought
> not).
> Perhaps better choices would be "nospam.example" or "nospam.invalid".
Yes, I'm sure. See <http://www.iana.org/domains/example/>
(<http://example.com> forwards to it).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@sdf.lonestar.org> |
|---|---|
| Date | 2012-07-19 14:11 +0000 |
| Message-ID | <slrn3vfsk0g5bo.122.ike@sdf.lonestar.org> |
| In reply to | #23394 |
On 2012-07-19, Keith Thompson <kst-u@mib.org> wrote: > Ike Naar <ike@sverige.freeshell.org> writes: >> On 2012-07-18, Keith Thompson <kst-u@mib.org> wrote: >>> Unless <nospam@nospam.com> is your real e-mail address, how do you >>> expect to get answers by e-mail? >>> >>> And if it isn't, you shouldn't use a real domain name as a fake address >>> (yes, there really is a nospam.com); it can result in the owners of that >>> domain receiving spam meant for you. "example.com" is guaranteed not to >>> exist. >> >> Are you sure about that? "example" does not exist as a top-level >> domain, but is it also a reserved name for subdomains? (I thought >> not). >> Perhaps better choices would be "nospam.example" or "nospam.invalid". > > Yes, I'm sure. See <http://www.iana.org/domains/example/> > (<http://example.com> forwards to it). Thanks. I was only aware of the reserved toplevel domains "example", "invalid", "test" and "localhost", and didn't know about "example.com", "example.org" and "example.net".
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-07-19 01:56 +0100 |
| Message-ID | <0.63bb9847db4020784082.20120719015604BST.87fw8owouz.fsf@bsb.me.uk> |
| In reply to | #23375 |
John May <nospam@nospam.com> writes:
> I keep getting this warning in GCC with the code segment below.
>
> file.c:256 "warning: dereferencing type-punned pointer will break
> strict-aliasing rules"
>
> /*****************************************/
> char buffer[SIZE];
> int length, type;
>
> /* Code to fill buffer */
>
> length = ntohs( *((unsigned short *)buffer) );
> type = ntohs( *((unsigned short *)buffer + 1) );
>
> /*****************************************/
>
>
> I'm trying to take the first two bytes (in network byte order) off the
> buffer and put them in the length, followed by the next two bytes into
> the type. GCC complains about the length conversion, but not the type.
>
> What does this warning mean, and should I bother with it?
gcc assumes (as it is permitted to do) that an object is only accessed
via an expression of "the right type" -- usually just the declared type
of the object. The objects in 'buffer' are declared to be chars, but
the pointer cast and dereference causes them to be accessed using an
expression of type short. Because the breaks gcc's assumptions it warns
you. It's generally a bad way to do this kind of thing.
(The phrase "the right type" is in quotes because the actual rules are
rather complex so my explanation is very much a simplification.)
An alternative is to use a union:
union {
uint8_t bytes[SIZE];
uint16_t words[SIZE/2];
} buf_type;
but_type buffer;
...
length = ntohs(buffer.words[0]);
type = ntohs(buffer.words[1]);
(The uintN_t types are standard C types from stdint.h.)
Alternatively you can construct the two values from the individual
bytes:
length = ((unsigned)buffer[0] << 8) + buffer[1];
but this is fiddly -- you should make the buffer an array of unsigned
char and you still need the cast before the shift for the code to be
properly portable.
> I don't want to keep checking this forum, so answers by email required.
> Quick responses appreciated.
Sorry -- impossible for reasons already explained.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2012-07-20 07:40 +0100 |
| Message-ID | <pan.2012.07.20.06.40.49.485000@nowhere.com> |
| In reply to | #23383 |
On Thu, 19 Jul 2012 01:56:03 +0100, Ben Bacarisse wrote: > length = ntohs(buffer.words[0]); > type = ntohs(buffer.words[1]); > > (The uintN_t types are standard C types from stdint.h.) Also, the type of ntohs() as specified by POSIX is: uint16_t ntohs(uint16_t);
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web