Check array size for overflow.

Since ZScript is a 32 bit VM, the largest safe value for an array's physical size is 2GB. Any larger value will be destroyed by the compiler which relies on signed 32 bit values too much.
This commit is contained in:
Christoph Oelckers 2024-10-25 08:47:19 +02:00 committed by Rachael Alexanderson
commit 2cbb980388
No known key found for this signature in database
GPG key ID: 26A8ACCE97115EE0

View file

@ -2286,6 +2286,11 @@ PType *ZCCCompiler::ResolveArraySize(PType *baseType, ZCC_Expression *arraysize,
Error(arraysize, "Array size must be positive");
return TypeError;
}
if (uint64_t(size) * baseType->Size > 0x7fffffff)
{
Error(arraysize, "Array size overflow. Total size must be less than 2GB");
return TypeError;
}
baseType = NewArray(baseType, size);
}