Assert when struct size would exceed 4 GB.

This commit is contained in:
Christoffer Lerno
2025-12-29 22:24:41 +01:00
parent 56f8008d85
commit c949bd3108
4 changed files with 16 additions and 0 deletions

View File

@@ -38,6 +38,7 @@
- Converting between simd/non-simd bool vector would hit a compiler assert. #2691
- `i<n>` suffixes were not caught when n < 8, causing an assert.
- Parse error in `$defined` was not handled correctly, leading to an assertion.
- Assert when struct size would exceed 4 GB.
### Stdlib changes
- Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads.

View File

@@ -637,7 +637,9 @@ static bool sema_analyse_struct_members(SemaContext *context, Decl *decl)
offset = align_offset;
member->offset = offset;
AlignSize sz = offset;
offset += type_size(member->type);
if (offset < sz || offset > MAX_STRUCT_SIZE) RETURN_SEMA_ERROR(member, "Struct member '%s' would cause the struct to become too large (exceeding 2 GB).", member->name);
}
// Set the alignment:

View File

@@ -29,6 +29,7 @@
#define DEFAULT_STACK_OBJECT_SIZE 64
#define MAX_ARRAY_SIZE INT64_MAX
#define MAX_SOURCE_LOCATION_LEN 255
#define MAX_STRUCT_SIZE (2U * 1024U * 1024U * 1024U)
#define PROJECT_JSON "project.json"
#define PROJECT_JSON5 "project.json5"

View File

@@ -0,0 +1,12 @@
import std;
struct Foo
{
int a;
int[int.max] b; // #error: "Struct member 'b' would cause the struct to become too large
}
fn int main()
{
Foo c = (Foo){};
return 0;
}