4 ms·
The behaviour of those functions is defined by the standard. The compiler is aware that realloc invalidates its pointer parameter.
by gpderetta 11y ago
The behaviour of those functions is defined by the standard. The compiler is aware that realloc invalidates its pointer parameter.
- masklinn 11y ago> The compiler is aware that realloc invalidates its pointer parameter. According to the standard, realloc doesn't invalidate its pointer parameter, since it can return the existing pointer (if the resizing could be done in-place) and can not touch the existing pointer (if the reallocation fails).
- gpderetta 11y agoAccording to the latest C standard draft that I could find, the semantics of realloc are that it deallocates the old object and allocates a new one. Of course, as an intended optimization, the new pointer value might have the same value as the old one, but that's not part of the semantics. The only case in which the old pointer is not invalidated is if realloc fails. As far as I know, in practice, at least with GCC, but likely clang as well, the compiler doesn't tracks the invalidation, but it simply assumes that any non null pointer returned by realloc (or any allocation function in fact) does not alias any other existing pointer in the program.
- mhogomchungu 11y agoThe pointer is invalidated only if the object is moved and the object is not always moved and hence the pointer is not always invalidated. http://pubs.opengroup.org/onlinepubs/7908799/xsh/realloc.html http://pubs.opengroup.org/onlinepubs/7908799/xsh/realloc.htm...
- derefr 11y agoDo note that I wasn't proposing to invalidate the memory-address handle contained in p; but rather to lexically invalidate p itself, such that it would no longer be legal for it to occur as an rvalue until a new value was assigned to it as an lvalue. You know, like Rust.
- shultays 11y agoIn this case it is easy to see that p is no longer valid but what about other pointers that points to the same location but not trivially visible for compilers? It won't be possible without run time checks or memory system like the one in rust.
- lmm 11y ago> It won't be possible without run time checks or memory system like the one in rust. So do that then.
- shultays 11y agorun time checks are run time checks, they add overhead. (imho) dangling pointers not worth the crippling the language like rust does.
- pcwalton 11y agoI've written hundreds of thousands of lines of working code in the "crippled" language you're referring to. And dangling pointers result in use-after-free, which frequently results in remote code execution vulnerabilities. Do you have a practical alternative to eliminating those RCE vulnerabilities, or should we accept them? To be clear, your position is an intellectually coherent one to take. But we should be clear about its implications.
- ben0x539 11y agoIsn't servo, which I assume where most of your Rust code lives by now, trading some safety to un-"cripple" Rust by having GC-backed smart pointers that allow for uses-after-free if they aren't used correctly?
- kibwen 11y agoServo integrates with a GC not because Rust is insufficiently expressive, but because it has to cooperatively manage the DOM with a garbage-collecting Javascript engine.
- pdw 11y agoThat's not how I interpret that. Both the specification of malloc() and realloc() contain this phrase: > The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated (until the space is explicitly freed or reallocated). (Emphasis added.) After realloc() succeeds, you can no longer use the original pointer.