Hello, time for an Undefined Behavior with context. The context is the XOR linked list, where a single integer is used to encode a forwards pointer and a backwards pointer. https://en.wikipedia.org/wiki/XOR_linked_list … #thread
-
Show this thread
-
It is a worry for the faithful compilation of that trick that GCC optimizes the function f as if it said “return 1;”. https://godbolt.org/g/522rXr pic.twitter.com/fJKl29CVqf
1 reply 1 retweet 13 likesShow this thread -
Note that only only thing that justifies compiling f the way GCC does is treating the main function (and the way it calls f) as undefined behavior.
2 replies 0 retweets 8 likesShow this thread -
“But sandy! The program *is* invoking undefined behavior,” you might be thinking. “Pointer arithmetic that goes out of bounds IS undefined. You should have used uintptr_t arithmetic.” I have some bad news… https://godbolt.org/g/E8EZSF pic.twitter.com/tPrPaMGSaK
3 replies 2 retweets 10 likesShow this thread -
The only thread still saving the XOR linked list is, well, the XOR. GCC currently acknowledges that p ^ o may not point to the same base address as p. https://godbolt.org/g/dEe4Av pic.twitter.com/WCcTHQRHBa
1 reply 1 retweet 7 likesShow this thread -
In the latter examples, the conversion from pointer to integer is implementation-defined and anything that happens after that may be undefined depending on the guarantees provided or not by the compiler.
2 replies 1 retweet 8 likesShow this thread -
Replying to @volatile_void
I don't see any justification for a claim that the implementation-defined result of the conversion can blow up into undefined behavior. If uintptr_t exists, it must be round-trip pointer-preserving.
1 reply 0 retweets 0 likes -
Replying to @RichFelker
“pointer provenance” uintptr_t p = (uintptr_t)&x; while (p++ != (uintptr_t) &y); does not have to terminate and uintptr_t p = rand(); if (p == (uintptr_t) &y) *p = 1; does not have to do what you expect.
1 reply 0 retweets 0 likes
"Pointer provenance" is utter bullshit.
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.