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

Commit

Permalink
Fixed an edge case where an array that didn't have a large enough all…
Browse files Browse the repository at this point in the history
…ocation was incorrectly being treated as if it did.
  • Loading branch information
Orvid committed Jan 13, 2015
1 parent 3c83c5c commit df15742
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/rt/lifetime.d
Expand Up @@ -218,17 +218,17 @@ private class ArrayAllocLengthLock
*/
bool __setArrayAllocLength(ref BlkInfo info, size_t newlength, bool isshared, const TypeInfo ti, size_t oldlength = ~0) pure nothrow
{
bool needToSetTypeInfo = false;
size_t typeInfoSize = 0;
if (auto si = cast(const TypeInfo_Struct)ti.next)
{
if (si.xdtor)
{
needToSetTypeInfo = true;
typeInfoSize = size_t.sizeof;
}
}
if(info.size <= 256)
{
if(newlength + SMALLPAD > info.size)
if(newlength + SMALLPAD + typeInfoSize > info.size)
// new size does not fit inside block
return false;
auto length = cast(ubyte *)(info.base + info.size - SMALLPAD);
Expand Down Expand Up @@ -259,15 +259,15 @@ bool __setArrayAllocLength(ref BlkInfo info, size_t newlength, bool isshared, co
// setting the initial length, no lock needed
*length = cast(ubyte)newlength;
}
if (needToSetTypeInfo)
if (typeInfoSize)
{
auto typeInfo = cast(TypeInfo_Struct*)(info.base + info.size - SMALLPAD - size_t.sizeof);
*typeInfo = cast(TypeInfo_Struct)ti.next;
}
}
else if(info.size < PAGESIZE)
{
if(newlength + MEDPAD > info.size)
if(newlength + MEDPAD + typeInfoSize > info.size)
// new size does not fit inside block
return false;
auto length = cast(ushort *)(info.base + info.size - MEDPAD);
Expand Down Expand Up @@ -298,7 +298,7 @@ bool __setArrayAllocLength(ref BlkInfo info, size_t newlength, bool isshared, co
// setting the initial length, no lock needed
*length = cast(ushort)newlength;
}
if (needToSetTypeInfo)
if (typeInfoSize)
{
auto typeInfo = cast(TypeInfo_Struct*)(info.base + info.size - MEDPAD - size_t.sizeof);
*typeInfo = cast(TypeInfo_Struct)ti.next;
Expand Down Expand Up @@ -337,7 +337,7 @@ bool __setArrayAllocLength(ref BlkInfo info, size_t newlength, bool isshared, co
// setting the initial length, no lock needed
*length = newlength;
}
if (needToSetTypeInfo)
if (typeInfoSize)
{
auto typeInfo = cast(TypeInfo_Struct*)(info.base + size_t.sizeof);
*typeInfo = cast(TypeInfo_Struct)ti.next;
Expand Down Expand Up @@ -698,11 +698,21 @@ Lcontinue:
{
curallocsize = *(cast(ubyte *)(info.base + info.size - SMALLPAD));
arraypad = SMALLPAD;
if (auto si = cast(const TypeInfo_Struct)ti.next)
{
if (si.xdtor)
arraypad += size_t.sizeof;
}
}
else if(info.size < PAGESIZE)
{
curallocsize = *(cast(ushort *)(info.base + info.size - MEDPAD));
arraypad = MEDPAD;
if (auto si = cast(const TypeInfo_Struct)ti.next)
{
if (si.xdtor)
arraypad += size_t.sizeof;
}
}
else
{
Expand Down Expand Up @@ -789,12 +799,27 @@ Lcontinue:
// assumes you are not counting the pad size, and info.size does include
// the pad.
if(info.size <= 256)
{
arraypad = SMALLPAD;
if (auto si = cast(const TypeInfo_Struct)ti.next)
{
if (si.xdtor)
arraypad += size_t.sizeof;
}
}
else if(info.size < PAGESIZE)
{
arraypad = MEDPAD;
if (auto si = cast(const TypeInfo_Struct)ti.next)
{
if (si.xdtor)
arraypad += size_t.sizeof;
}
}
else
arraypad = LARGEPAD;


curcapacity = info.size - arraypad;
return curcapacity / size;
}
Expand Down

0 comments on commit df15742

Please sign in to comment.