This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 411
Fix issue 19128 - argument to alloca may be too large #2258
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,8 +30,25 @@ extern (C) void[] _d_arrayassign(TypeInfo ti, void[] from, void[] to) | |
|
|
||
| // Need a temporary buffer tmp[] big enough to hold one element | ||
| void[16] buf = void; | ||
| void* ptmp = (elementSize > buf.sizeof) ? alloca(elementSize) : buf.ptr; | ||
| return _d_arrayassign_l(ti, from, to, ptmp); | ||
| void* ptmp; | ||
| if (elementSize <= buf.sizeof) | ||
| { | ||
| ptmp = buf.ptr; | ||
| } | ||
| else if (elementSize < 1024) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How was 1024 chosen?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty random. Should be enough for a lot of data types, and not too much. |
||
| { | ||
| ptmp = alloca(elementSize); | ||
| } | ||
| else | ||
| { | ||
| ptmp = malloc(elementSize); | ||
| } | ||
| auto dst = _d_arrayassign_l(ti, from, to, ptmp); | ||
| if (elementSize >= 1024) | ||
| { | ||
| free(ptmp); | ||
| } | ||
| return dst; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -209,12 +226,18 @@ extern (C) void* _d_arraysetassign(void* p, void* value, int count, TypeInfo ti) | |
| //Need a temporary buffer tmp[] big enough to hold one element | ||
| void[16] buf = void; | ||
| void[] tmp; | ||
| if (element_size > buf.sizeof) | ||
| if (element_size <= buf.sizeof) | ||
| { | ||
| tmp = buf[]; | ||
| } | ||
| else if (element_size < 1024) | ||
| { | ||
| tmp = alloca(element_size)[0 .. element_size]; | ||
| } | ||
| else | ||
| tmp = buf[]; | ||
| { | ||
| tmp = malloc(element_size)[0 .. element_size]; | ||
| } | ||
|
|
||
| foreach (i; 0 .. count) | ||
| { | ||
|
|
@@ -224,6 +247,10 @@ extern (C) void* _d_arraysetassign(void* p, void* value, int count, TypeInfo ti) | |
| ti.destroy(tmp.ptr); | ||
| p += element_size; | ||
| } | ||
| if (element_size >= 1024) | ||
| { | ||
| free(tmp.ptr); | ||
| } | ||
| return pstart; | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue here that if we are going to fall back to malloc. Then having a pre-allocated buffer is pointless.
This should be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or at the very least, this should just be a simple
s/alloca/malloc/swap.