Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is adding a uintptr_t to a pointer symmetric?

Preface: I understand that use cases for this are challenging to find. This question is purely theoretical and arose by curiosity.

Take the following:

char b[SOME_SIZE];

Is this:

((char *)5) + ((uintptr_t)b)

Guaranteed to yield the same result as the more conventional:

(b) + ((uintptr_t)5)

Provided the implementation defines uintptr_t?

If I add a 'pointer' (containing an offset) to a uintptr_t containing an address, will the result be as expected? I.e., the same as if I added the offset to the address directly?

like image 215
CPlus Avatar asked Aug 25 '26 12:08

CPlus


2 Answers

Strictly speaking, this is undefined behavior.

Performing pointer arithmetic, specifically adding a value to a pointer, is only valid if the pointer points to a member of a valid array object (or a single object) and the result points to a member of that same array object. This is spelled out in section 6.5.6p8 of the C standard regarding Additive Operators:

When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

So in the case of this expression:

((char *)5) + ((uintptr_t)b)

The pointer expression (char *)5 doesn't point to a valid object (unless you're on a system that explicitly allows using "5" as a valid address), so performing arithmetic on it is undefined.

On a system where an address is simply a single 32-bit or 64-bit value this will probably work, but there's no guarantee of that. Some compilers have the concept of pointer provenance, where it keeps track of the object a pointer points to, and such a construct may run afoul of compilers that use this concept when performing optimizations.

I've seen examples of a pair of pointers a and b where (uintptr_t)a == (uintptr_t)b is true but a == b is false.

like image 126
dbush Avatar answered Aug 27 '26 02:08

dbush


In addition to @dbush good answer:

(uintptr_t)b) does not certainly form an integer that is arithmetically meaningful for addition.

(uintptr_t)b) converts b (or more precisely the address of the first element of array b) to an integer. Converting that number back to a char * as in (char *)(uintptr_t)b yields a pointer that is equivalent to &b[0] is well defined.

Consider (uintptr_t)&b[0]) may yield 0x12340010 and (uintptr_t)&b[1]) may yield 0x12340018. The pointer difference &b[1] - &b[0] is 1, but the difference of the (uintptr_t)&b[1]) - (uintptr_t)&b[0]) is 0x08. There is no defined math relationship to the bits of (uintptr_t)&b[1]) and (uintptr_t)&b[0]) other than the values must differ.

With the integer formed from (uintptr_t)b, there is very limited arithmetic that can be done on it with meaning.

like image 32
chux - Reinstate Monica Avatar answered Aug 27 '26 04:08

chux - Reinstate Monica