Skip to content

Commit 61bc355

Browse files
committed
Fix arena allocator alignment for 64-bit hosts
The arena allocator was using a hardcoded PTR_SIZE (4 bytes) for memory alignment, which caused misalignment issues on 64-bit host systems where pointers are 8 bytes. This led to UndefinedBehaviorSanitizer errors.
1 parent 208ad57 commit 61bc355

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/globals.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ void *arena_alloc(arena_t *arena, int size)
164164
abort();
165165
}
166166

167-
/* Align to PTR_SIZE bytes */
168-
size = (size + PTR_SIZE - 1) & ~(PTR_SIZE - 1);
167+
/* Align to sizeof(void*) bytes for host compatibility */
168+
int alignment = sizeof(void *);
169+
size = (size + alignment - 1) & ~(alignment - 1);
169170

170171
if (!arena->head || arena->head->offset + size > arena->head->capacity) {
171172
/* Need a new block: choose capacity = max(DEFAULT_ARENA_SIZE,

0 commit comments

Comments
 (0)