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

Commit ab890e0

Browse files
authored
Merge pull request #2409 from ibuclaw/pr2258
Fix issue 19128 - argument to alloca may be too large
2 parents 286479b + f6ef2b8 commit ab890e0

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/rt/arrayassign.d

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ extern (C) void[] _d_arrayassign(TypeInfo ti, void[] from, void[] to)
3030

3131
// Need a temporary buffer tmp[] big enough to hold one element
3232
void[16] buf = void;
33-
void* ptmp = (elementSize > buf.sizeof) ? alloca(elementSize) : buf.ptr;
33+
void* ptmp = (elementSize > buf.sizeof) ? malloc(elementSize) : buf.ptr;
34+
scope (exit)
35+
{
36+
if (ptmp != buf.ptr)
37+
free(ptmp);
38+
}
3439
return _d_arrayassign_l(ti, from, to, ptmp);
3540
}
3641

@@ -206,24 +211,20 @@ extern (C) void* _d_arraysetassign(void* p, void* value, int count, TypeInfo ti)
206211

207212
auto element_size = ti.tsize;
208213

209-
//Need a temporary buffer tmp[] big enough to hold one element
210-
void[16] buf = void;
211-
void[] tmp;
212-
if (element_size > buf.sizeof)
213-
{
214-
tmp = alloca(element_size)[0 .. element_size];
215-
}
216-
else
217-
tmp = buf[];
214+
// Need a temporary buffer tmp[] big enough to hold one element
215+
immutable maxAllocaSize = 512;
216+
void *ptmp = (element_size > maxAllocaSize) ? malloc(element_size) : alloca(element_size);
218217

219218
foreach (i; 0 .. count)
220219
{
221-
memcpy(tmp.ptr, p, element_size);
220+
memcpy(ptmp, p, element_size);
222221
memcpy(p, value, element_size);
223222
ti.postblit(p);
224-
ti.destroy(tmp.ptr);
223+
ti.destroy(ptmp);
225224
p += element_size;
226225
}
226+
if (element_size > maxAllocaSize)
227+
free(ptmp);
227228
return pstart;
228229
}
229230

0 commit comments

Comments
 (0)