Skip to content

Commit 72ade5f

Browse files
Zero-initialize hart_t using calloc
Uninitialized hart_t fields may lead to undefined behavior or startup exceptions. This change replaces malloc with calloc to ensure that all fields are zeroed upon allocation, providing a safer and more consistent initialization. Co-authored-by: Po-Ying Chiu <[email protected]> Reported-by: @testriscv
1 parent 430b960 commit 72ade5f

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,11 @@ static int semu_init(emu_state_t *emu, int argc, char **argv)
650650
vm->n_hart = hart_count;
651651
vm->hart = malloc(sizeof(hart_t *) * vm->n_hart);
652652
for (uint32_t i = 0; i < vm->n_hart; i++) {
653-
hart_t *newhart = malloc(sizeof(hart_t));
653+
hart_t *newhart = calloc(1, sizeof(hart_t));
654+
if (!newhart) {
655+
fprintf(stderr, "Failed to allocate hart #%u.\n", i);
656+
return 1;
657+
}
654658
INIT_HART(newhart, emu, i);
655659
newhart->x_regs[RV_R_A0] = i;
656660
newhart->x_regs[RV_R_A1] = dtb_addr;

0 commit comments

Comments
 (0)