Skip to content

Commit d046525

Browse files
tridgeclaude
andcommitted
zero all new memory from allocations
Change my_alloc() to use calloc instead of malloc so all fresh allocations return zeroed memory. Also zero the expanded portion in expand_item_list() after realloc, since it knows both old and new sizes. This gives more predictable behaviour in case of bugs where uninitialised or stale memory is accidentally accessed. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent bb0a811 commit d046525

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

util1.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,8 @@ void *expand_item_list(item_list *lp, size_t item_size, const char *desc, int in
17181718
new_ptr == lp->items ? " not" : "");
17191719
}
17201720

1721+
memset((char *)new_ptr + lp->malloced * item_size, 0,
1722+
(expand_size - lp->malloced) * item_size);
17211723
lp->items = new_ptr;
17221724
lp->malloced = expand_size;
17231725
}

util2.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ void *my_alloc(void *ptr, size_t num, size_t size, const char *file, int line)
7979
who_am_i(), do_big_num(max_alloc, 0, NULL), src_file(file), line);
8080
exit_cleanup(RERR_MALLOC);
8181
}
82-
if (!ptr)
83-
ptr = malloc(num * size);
84-
else if (ptr == do_calloc)
82+
if (!ptr || ptr == do_calloc)
8583
ptr = calloc(num, size);
8684
else
8785
ptr = realloc(ptr, num * size);

0 commit comments

Comments
 (0)