fix unsafe .ptr accesses in druntime#1590
Conversation
5a9260a to
cf1ee63
Compare
cf1ee63 to
ac3775f
Compare
|
|
||
| private const(void)* arrayToPtr(const void[] array) @trusted | ||
| { | ||
| // Ok because the user will never dereference the pointer |
There was a problem hiding this comment.
Not ok... If safe code can call this method and thereby break memory safety, it should not be trusted. The fact that the callers above don't do this is just a convention - they are effectively @trusted thanks to this function.
There was a problem hiding this comment.
What about this instead:
private size_t arrayToPtr(const void[] array) @trusted { return cast(size_t)array.ptr; }There was a problem hiding this comment.
Hm... probably should return ptrdiff_t instead of size_t, since pointer arithmetic is signed.
There was a problem hiding this comment.
Sure, anything that prevents it from being dereferenced in @safe code.
There was a problem hiding this comment.
probably should return ptrdiff_t
I take it back, needs to be size_t because of the code in _enforceNoOverlap
|
@yebblies we had a spirited debate about this a couple years ago, and I started out in your position. A consensus eventually emerged that @trusted code should be shrink-wrapped around the unsafe operation, so that safety inference can be properly done with the rest of the code, even though the rest of the code still has to behave itself. You'll see this in other places with paired malloc/free in separate @trusted blocks. I made the function @schveiguy I've used machines where size_t is not the same size as void*, even in 32 bit mode. The trouble with assuming they are the same size is some very weird bugs can silently appear if such a machine is ported to. I have developed an aversion to such equivalences after having my kingdom blasted by them. |
|
I think there were two parts to that discussion - one was using The whole idea that |
ac3775f to
7b80a15
Compare
|
I just realized instead of |
|
Auto-merge toggled on |
|
Thanks, I had not heard of Side question: will D ever support such systems? I have worked with 8-bit CPUs where the pointer type is 16-bits (D could potentially support this, but never druntime), but never seen something of modern 32-bit or higher that does this. |
|
Hm.. |
I think we need something like this at least for porting existing code. Although maybe named a bit clearer e.g. edit: Or if needed in druntime, maybe a new module, core.compat. std.typecons.octal could be moved there, maybe other things would fit too. |
|
Could someone please explain |
Walter's preferences... And possibly some vague future plans to change what |
array.ptr is no longer considered @safe
Needed for dlang/dmd#5860