diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-04 18:28:18 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-04 18:28:18 -0400 |
| commit | 8e2156ac483e7fa0f2007188b670eb11f2a44720 (patch) | |
| tree | f6fcbb022fee0d3ad060c2429ae465aaed17ba7d /builtins | |
| parent | 73000498674ea2a72204c0e137c45bdfa7afd524 (diff) | |
Guard against tables exceeding maximum supported size
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/datatypes.h | 1 | ||||
| -rw-r--r-- | builtins/table.c | 4 |
2 files changed, 5 insertions, 0 deletions
diff --git a/builtins/datatypes.h b/builtins/datatypes.h index e73e3c16..f1aa5747 100644 --- a/builtins/datatypes.h +++ b/builtins/datatypes.h @@ -34,6 +34,7 @@ typedef struct { uint32_t next_bucket; } bucket_t; +#define TABLE_MAX_BUCKETS 0x7fffffff #define TABLE_MAX_DATA_REFCOUNT 3 typedef struct { diff --git a/builtins/table.c b/builtins/table.c index f3f97ec3..a1ce610c 100644 --- a/builtins/table.c +++ b/builtins/table.c @@ -199,6 +199,8 @@ static void Table$set_bucket(table_t *t, const void *entry, int32_t index, const static void hashmap_resize_buckets(table_t *t, uint32_t new_capacity, const TypeInfo *type) { + if (__builtin_expect(new_capacity > TABLE_MAX_BUCKETS, 0)) + fail("Table has exceeded the maximum table size (2^31) and cannot grow further!"); hdebug("About to resize from %u to %u\n", t->bucket_info ? t->bucket_info->count : 0, new_capacity); hshow(t); int64_t alloc_size = sizeof(bucket_info_t) + sizeof(bucket_t[new_capacity]); @@ -247,6 +249,8 @@ public void *Table$reserve(table_t *t, const void *key, const void *value, const // Resize buckets if necessary if (t->entries.length >= (int64_t)t->bucket_info->count) { uint32_t newsize = t->bucket_info->count + MIN(t->bucket_info->count, 64); + if (__builtin_expect(newsize > TABLE_MAX_BUCKETS, 0)) + newsize = t->entries.length + 1; hashmap_resize_buckets(t, newsize, type); } |
