libdrgn: document implementation-defined behavior in add_to_possibly_null_pointer()

Konrad Borowski pointed out that add_to_possibly_null_pointer() relies
on GCC-specific behavior:
https://fosstodon.org/@xfix/109542070338182493. CONTRIBUTING.rst
mentions that we assume that casting between pointers and integers does
not change the bit representation, but we might as well document it
here, too.

Signed-off-by: Omar Sandoval <osandov@osandov.com>
This commit is contained in:
Omar Sandoval 2022-12-19 12:07:40 -08:00
parent 7b64aee4dd
commit aa5f121ac9

View File

@ -182,7 +182,10 @@ static inline uint64_t uint_max(int n)
*
* A more natural definition would be `i == 0 ? ptr : ptr + i`, but some
* versions of GCC and Clang generate an unnecessary branch or conditional move
* (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97225).
* (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97225). Note that in standard
* C, it is undefined behavior to cast to `uintptr_t`, do arithmetic, and cast
* back, but GCC allows this as long as the result is within the same object:
* https://gcc.gnu.org/onlinedocs/gcc/Arrays-and-pointers-implementation.html.
*/
#define add_to_possibly_null_pointer(ptr, i) \
((typeof(ptr))((uintptr_t)(ptr) + (i) * sizeof(*(ptr))))