Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
util.container.array: Add invariant for _ptr/_length
Browse files Browse the repository at this point in the history
This was quite useful for debugging a memory corruption issue.
  • Loading branch information
dnadlinger committed Sep 11, 2016
1 parent d6924a9 commit 3e95dbf
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/rt/util/container/array.d
Expand Up @@ -39,11 +39,11 @@ nothrow:
size_t reqsize = mulu(T.sizeof, nlength, overflow);
if (!overflow)
{
if (nlength < length)
foreach (ref val; _ptr[nlength .. length]) common.destroy(val);
if (nlength < _length)
foreach (ref val; _ptr[nlength .. _length]) common.destroy(val);
_ptr = cast(T*)common.xrealloc(_ptr, reqsize);
if (nlength > length)
foreach (ref val; _ptr[length .. nlength]) common.initialize(val);
if (nlength > _length)
foreach (ref val; _ptr[_length .. nlength]) common.initialize(val);
_length = nlength;
}
else
Expand Down Expand Up @@ -130,6 +130,11 @@ nothrow:
other._length = len;
}

invariant
{
assert(!_ptr == !_length);
}

private:
T* _ptr;
size_t _length;
Expand Down Expand Up @@ -209,7 +214,7 @@ unittest
try
{
// Overflow ary.length.
auto ary = Array!size_t(null, -1);
auto ary = Array!size_t(cast(size_t*)0xdeadbeef, -1);
ary.insertBack(0);
}
catch(OutOfMemoryError)
Expand All @@ -218,7 +223,7 @@ unittest
try
{
// Overflow requested memory size for common.xrealloc().
auto ary = Array!size_t(null, -2);
auto ary = Array!size_t(cast(size_t*)0xdeadbeef, -2);
ary.insertBack(0);
}
catch(OutOfMemoryError)
Expand Down

0 comments on commit 3e95dbf

Please sign in to comment.