3 ms·
Their solution is not undefined. After casting to uintptr_t, the value is an unsigned integer which has well-defined overflow behavior. The casting itself is ok
by ssp 17y ago
Their solution is not undefined. After casting to uintptr_t, the value is an unsigned integer which has well-defined overflow behavior. The casting itself is okay too because uintptr_t is guaranteed to be able to hold a pointer.
- dmm 17y agoTheir solution is well-defined but depends on undefined behavior. My understanding is that the casting is simply to prevent the compiler from optimizing out the check. if((uintptr_t)buf+len < (uintptr_t)buf) might fail to give the expected result if the address space is segmented.
- loup-vaillant 17y agoThe problem is that `buf+len` is still a `char*`. So it could still trigger undefined behaviour before it is converted to `uintPtr_t`. The real solution is to have `buf` declared as a `unitPtr_t` in the first place. Ugly, but standard.
- ssp 17y agoNo, (uintptr_t)buf + len is parenthesized like this: ((uintptr_t)buf) + len.
- loup-vaillant 17y agoWhoops, my mistake. Forget what I said.