RFH: aliasing problems

Steven Johnson sjohnson at sakuraindustries.com
Mon Dec 11 06:26:54 UTC 2006


Hi,

Can anyone explain how -fstrict-aliasing is compatible with C99 Section
6.3.2.3(7).

because it says:

"A pointer to an object ... may be converted to a pointer to a different
object ... when converted back again the results shall compare equal to
the original pointer."

If this is not the definition of type punning with pointers, I do not
know what is.

To my understanding, this rule is incompatible with -fstrict-aliasing,
and so, such an optimisation that prevents this behaviour would not
follow C99. Accordingly assigning between sockaddr to sockaddr_xx is
specifically allowed by C99.  Any compiler that doesn't allow this is
not following 6.3.2.3 (7).

Steven J



Thomas Doerfler wrote:
> Ralf,
>
> Ralf Corsepius schrieb:
>   
>> Neither Linux nor Solaris-2.7 exhibit this behavior with their sockaddr*
>> types and gcc-4.1.x. FreeBSD and RTEMS do.
>>
>>
>> Linux, Solaris and Cygwin have
>>
>> struct sockaddr {
>>   sa_family_t sa_family;
>>   char sa_data[14];
>> };
>>
>> struct sockaddr_xx {
>>   sa_family_t sa_family;
>>   ....
>> };
>>     
>
>
>   
>> FreeBSD and RTEMS have:
>>
>> struct sockaddr {
>>   unsigned char	sa_len;
>>   sa_family_t sa_family;
>>   char sa_data[14];
>> };
>>
>> struct sockaddr_xx {
>>   unsigned char sa_len;
>>   sa_family_t sa_family;
>>   ...
>> };
>>
>>
>> So, how does this match with strict-aliasing rules?
>>
>>     
>
> When I look at the above structures, I think they are definitively
> incompatible regarding the aliasing rule. If some kernel code converts a
> sockaddr_xx pointer to a more general sockaddr pointer, C99 assumes that
> they do not alias. I do not see a principal difference to the
> FreeBSD/RTEMS definitions of these structures, except we have an
> additional general element "sa_len".
>
> When I look into the kernel makefiles for my linux system (2.6.13), the
> kernel obviously is built with "-fno-strict-aliasing". Maybe this is not
> needed for all (clean) applications, but then the application code
> should use either "struct sockaddr" or "struct sockaddr_xx", but not
> intermix pointers to both types of structures.
>
> Once again, I see no basic difference in the structure implementations.
> Both implementations make the two structures incompatible concerning
> aliasing.
>
> wkr
> Thomas.
>
>
>   
>> Ralf
>>
>>
>> _______________________________________________
>> rtems-users mailing list
>> rtems-users at rtems.com
>> http://rtems.rtems.org/mailman/listinfo/rtems-users
>>     
>
>
>   




More information about the users mailing list