On 7/30/2012 4:04 PM, China Blue [Tor], Meersburg wrote:
> In article <7edfdd82-a84a-4391-8a63->,
> Fred K <> wrote:
>
>> The Xpm pixmap library contains code that casts an unsigned int
>> to a (void *), passing that as an argument to a function
>> that expects a (void *):
>>
>> /* Function prototype: */
>> xpmHashIntern (xpmHashTable *table, char *tag, void *data));
>>
>> /* Calling it, casting 3rd argument: */
>> int a = <something>;
>> xpmHashIntern(hashtable, color->string, (void *)a );
>>
>> On a 64-bit platform where pointers are 64 bits and ints
>> are 32 bits, is this safe?
>
> intptr_t is an integer which is large enough to hold a void* pointer encoded in
> an integer.
intptr_t and uintptr_t are both irrelevant to the O.P.'s
question. The fact that a void* can be converted to a [u]intptr_t
which can be in turn converted to a void* with the two pointers
comparing equal does not imply that a [u]intptr_t can be converted
to a void* and then back to a [u]intptr_t with the two integers
equal.
P1 -> I -> P2, P2 == P1
I1 -> P -> I2, I2 ?= I1 (no guarantee)
> This is an ancient bug
Makes no sense. It can't be any more ancient than C99, which
introduced the types. "Bug" can be a subjective judgement, but it's
certainly debatable whether the existence of [u]intptr_t is a "bug."
People who want to convert pointers to integers and back probably
dispute your assessment.
> dating back to the days when an integer was
> 60 bits, would always be 60 bits, and an address was 18 bits, and would always
> be 18 bits. The 80x86s had addresses (20 bits) larger than ints (16 bits),
> exposing this bad assumption.
Is this supposed to be an argument that [u]intptr_t is a "bug?"
I cannot comprehend what it's supposed to be about.
> The intptr_t was eventually introduced to provide
> an integer that would always be safe to hold addresses.
Not quite. The [u]intptr_t types are optional, meaning
that they are not "always" safe to hold addresses. Also, there
has been no introduction of addresses that are safe to hold
integers. Finally, none of this is "ancient" except to pre-teens.
--
Eric Sosman
d