Skip to content

Commit 48b1820

Browse files
authored
fix(GC): Fix #22004 - Setting array length of valid trailing slice (#22009)
should not reallocate if it will fit in the block.
1 parent 9289ab4 commit 48b1820

File tree

1 file changed

+26
-1
lines changed
  • druntime/src/core/internal/gc/impl/conservative

1 file changed

+26
-1
lines changed

druntime/src/core/internal/gc/impl/conservative/gc.d

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ class ConservativeGC : GC
15131513
auto existingUsed = slice.length + offset;
15141514

15151515
size_t typeInfoSize = (info.attr & BlkAttr.STRUCTFINAL) ? size_t.sizeof : 0;
1516-
if (__setArrayAllocLengthImpl(info, offset + newUsed, atomic, existingUsed, typeInfoSize))
1516+
if (__setArrayAllocLengthImpl(info, newUsed, atomic, existingUsed, typeInfoSize))
15171517
{
15181518
// could expand without extending
15191519
if (!bic && !atomic)
@@ -5397,6 +5397,31 @@ unittest
53975397
}
53985398
}
53995399

5400+
// https://github.com/dlang/dmd/issues/22004
5401+
@safe unittest
5402+
{
5403+
outer:
5404+
foreach (i; 1 .. 100)
5405+
{
5406+
foreach (j; 0 .. i)
5407+
{
5408+
int[] orig;
5409+
orig.length = i;
5410+
if (orig.capacity == orig.length)
5411+
// skip legitimate realloc cases
5412+
continue outer;
5413+
5414+
int[] slice = orig[j .. $];
5415+
assert(slice.capacity != 0);
5416+
5417+
auto ptr = &slice[0];
5418+
slice.length += 1;
5419+
// should not have realloc'd
5420+
assert(&slice[0] is ptr);
5421+
}
5422+
}
5423+
}
5424+
54005425
// https://github.com/dlang/dmd/issues/21615
54015426
debug(SENTINEL) {} else // no additional capacity with SENTINEL
54025427
version (OnlyLowMemUnittests) {} else

0 commit comments

Comments
 (0)