Hacker News new | ask | show | jobs
by uecker 12 days ago
There are stricter requirements for casting a void pointer to (u)intptr_t and back. Again, a segmented architecture should not provide (u)intptr_t. But if an implementation has it, in my opinion, this is expected to work.
1 comments

The only stricter requirement in the standard is that they compare equal after the round trip.

The intended purpose of these types is to allow the round trip, yes, and the requirement they compare equal is part of that. The standard doesn't put stricter requirements. I don't think that is an oversight, but a deliberate omission to allow for flexibility in implementation.

It's up to the compiler to specify that it's safe to dereference the pointer after the round trip - the standard does not want to put such constraints on implementors.

This is where I disagree. In the context when this was written, I think it was clear that two pointers that compare equally were meant to be interchangeable.
It would've been trivial for the committee to add such clause in the spec - why only the requirement that they compare equal and not that they dereference the same object?

The standard leaves a lot of things implementation defined deliberately to allow for flexibility in both processor design and compiler implementation.

I don't think for example, a segmented architecture shouldn't provide an `intptr_t` - it's still useful to be able to cast a pointer to integer and back even in the case of segmentation - but is obviously more prone to error if we dereference it in a different segment (which would be UB).

I completely agree that it is the intended case that a pointer that has had this round trip is intended to be dereferenced - but I disagree that the requirement to do so is an oversight and not a deliberate omission.

There are potential cases where you would want to dereference the pointer but to a different object (which is UB by the standard, but could be well defined for a specific compiler/arch). Consider for example, if you have a bunch of threads which all have the same structure in their thread local storage - it can be useful to be able to specify an offset that is going fetch a value specific value from the TLS - but obviously a different one in each thread - ie, we want the pointer to be an offset, and not an absolute address.

It is not trivial for anything to be done in a committee. If we wrote this text today, we would clearly specify this differently, but in the 90s there was not much reason to assume that this will be misunderstood. Note also that the far / near pointer model used by segmented architectures was never part of ISO C.

The committee wanted to have TS 6010 which would clarify that this work, but at the request of compiler vendors it is not (yet) part of the IS. But the reasons are unrelated to pointer round trips.

It would have been trivial, given foresight of such an argument. "They didn't specifically reject my interpretation of this document when they wrote the document" isn't a good argument for your interpretation, especially when it's a particularly counterintuitive one.
The standard is quite specific about what occurs when casting between integers and pointers.

> An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, may not be correctly aligned, may not point to an entity of the referenced type, and can produce an indeterminate representation when stored into an object.

> Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result is not required to be in the range of values of any integer type.

Why would these passages be written with such specificity, only to be overridden by unwritten passages w.r.t intptr_t?

When intptr_t was added, for C99, segmented memory was not uncommon, and there was more variation in processor architecture. The reason this is IDB or UB is because there were architectures where the implementation of such conversions is not straightforward.

Today we've largely converged on a common model of a single virtual address space with paging - if it were written today, it probably would have included such clauses, or made more assumptions about conversion between int/pointer rather than explicitly stating that nearly all behavior in this regard is IDB or UB, because the architectures these clauses were written to address - where implementation is non-obvious - are pretty much obsolete.

Anything besides this is subjective "interpretation" of the spec. There's nothing about `intptr_t` having different properties - the intptr_t is sufficiently wide to hold a pointer converted to integer - it solves only the last issue that "the result is not required to fit in the range of values of any integer type" - and hence why `intptr_t` is optional. It says nowhere that its purpose is to ensure that an intptr_t converted back to can be dereferenced without IDB. It's a pointer converted from an integer which is still subject to the constraints specified for int->pointer and pointer->int conversion.

Again, I don't challenge the idea that `void*->intptr_t->void*` is intended to result in a usable pointer - but I reject the idea that it was some kind of oversight to not explicitly state it in the spec - when the spec explicitly states the opposite - that it's IDB. It's implementation-defined because it was intended to be used in such way where pointers are effectively integers, but they did not want to place constraints on compilers which targetted the other architectures where making this guarantee would've been unpractical.