Skip to content

Commit fba134d

Browse files
cktanclaude
andcommitted
Use offsetof() in page_create() instead of null-pointer member access (#56)
The hand-rolled offsetof applied -> to a null pointer, which is undefined behavior and is reported by clang's UBSan at -Og. offsetof() computes the same size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8a4564e commit fba134d

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

‎src/tomlc17.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <math.h>
1010
#include <stdarg.h>
1111
#include <stdbool.h>
12+
#include <stddef.h>
1213
#include <stdint.h>
1314
#include <stdio.h>
1415
#include <stdlib.h>
@@ -107,7 +108,7 @@ static page_t *page_create(int size) {
107108
if (!(0 <= size && size <= (1 << 30))) { // [0..1GB]
108109
return NULL;
109110
}
110-
size_t totalsz = (size_t) & ((page_t *)0)->data[size];
111+
size_t totalsz = offsetof(page_t, data) + (size_t)size;
111112
page_t *page = MALLOC(totalsz);
112113
if (!page) {
113114
return NULL;

0 commit comments

Comments
 (0)