Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/internal/string.d
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ auto signedToTempString(long value, char[] buf, uint radix) @safe
if(neg)
{
// about to do a slice without a bounds check
assert(r.ptr > buf.ptr);
r = (() @trusted => (r.ptr-1)[0..r.length+1])();
auto trustedSlice() @trusted { assert(r.ptr > buf.ptr); return (r.ptr-1)[0..r.length+1]; }
r = trustedSlice();
r[0] = '-';
}
return r;
Expand Down
27 changes: 17 additions & 10 deletions src/rt/util/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@ module rt.util.array;


import core.internal.string;
import core.stdc.stdint;


@safe /* pure dmd @@@BUG11461@@@ */ nothrow:

void enforceTypedArraysConformable(T)(in char[] action,
in T[] a1, in T[] a2, in bool allowOverlap = false)
void enforceTypedArraysConformable(T)(const char[] action,
const T[] a1, const T[] a2, in bool allowOverlap = false)
{
_enforceSameLength(action, a1.length, a2.length);
if(!allowOverlap)
_enforceNoOverlap(action, a1.ptr, a2.ptr, T.sizeof * a1.length);
_enforceNoOverlap(action, arrayToPtr(a1), arrayToPtr(a2), T.sizeof * a1.length);
}

void enforceRawArraysConformable(in char[] action, in size_t elementSize,
in void[] a1, in void[] a2, in bool allowOverlap = false)
void enforceRawArraysConformable(const char[] action, in size_t elementSize,
const void[] a1, const void[] a2, in bool allowOverlap = false)
{
_enforceSameLength(action, a1.length, a2.length);
if(!allowOverlap)
_enforceNoOverlap(action, a1.ptr, a2.ptr, elementSize * a1.length);
_enforceNoOverlap(action, arrayToPtr(a1), arrayToPtr(a2), elementSize * a1.length);
}

private void _enforceSameLength(in char[] action,
private void _enforceSameLength(const char[] action,
in size_t length1, in size_t length2)
{
if(length1 == length2)
Expand All @@ -46,10 +47,10 @@ private void _enforceSameLength(in char[] action,
throw new Error(msg);
}

private void _enforceNoOverlap(in char[] action,
in void* ptr1, in void* ptr2, in size_t bytes)
private void _enforceNoOverlap(const char[] action,
uintptr_t ptr1, uintptr_t ptr2, in size_t bytes)
{
const size_t d = ptr1 > ptr2 ? ptr1 - ptr2 : ptr2 - ptr1;
const d = ptr1 > ptr2 ? ptr1 - ptr2 : ptr2 - ptr1;
if(d >= bytes)
return;
const overlappedBytes = bytes - d;
Expand All @@ -63,3 +64,9 @@ private void _enforceNoOverlap(in char[] action,
msg ~= bytes.unsignedToTempString(tmpBuff, 10);
throw new Error(msg);
}

private uintptr_t arrayToPtr(const void[] array) @trusted
{
// Ok because the user will never dereference the pointer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this instead:

private size_t arrayToPtr(const void[] array) @trusted { return cast(size_t)array.ptr; }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... probably should return ptrdiff_t instead of size_t, since pointer arithmetic is signed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, anything that prevents it from being dereferenced in @safe code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably should return ptrdiff_t

I take it back, needs to be size_t because of the code in _enforceNoOverlap

return cast(uintptr_t)array.ptr;
}