Fix a problem where zero-initializing a one-byte array would crash the system.

Also, generate better code for zero-initializing small arrays.

The problem was that the code would call the library routine ~ZERO with a size of 1, but it only works properly with a size of 2 or more. While adding a check here, I also changed it to not call ~ZERO for other small arrays (<=10 bytes), since it is generally more efficient to just initialize them directly.

The initializations in the following are examples that could trigger the problem:

int main(void)
{
    struct { int i; char s[1]; } foo = {1, 0};
    char arr[2][1] = {2};
}
This commit is contained in:
Stephen Heumann 2016-10-16 19:22:29 -05:00
parent fa5974199d
commit 972b0109a4
1 changed files with 3 additions and 1 deletions

View File

@ -3662,7 +3662,9 @@ procedure DoStatement;
with iPtr^.itree^ do
if token.kind = intconst then
if token.ival = 0 then
ZeroFill := true;
{don't call ~ZERO for very small arrays}
if elements * itype^.size > 10 then
ZeroFill := true;
end; {ZeroFill}