Skip to content

Commit

Permalink
Avoiding null pointers in the init/fini arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasnoble committed Dec 22, 2022
1 parent 1c8e0b3 commit 69e0687
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions libc/src/initfini.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@ static void __libc_init_array() {
size_t count, i;

count = __preinit_array_end - __preinit_array_start;
for (i = 0; i < count; i++)
__preinit_array_start[i]();
for (i = 0; i < count; i++) {
void (*ptr)() = __preinit_array_start[i];
if (ptr) ptr();
}

count = __init_array_end - __init_array_start;
for (i = 0; i < count; i++)
__init_array_start[i]();
for (i = 0; i < count; i++) {
void (*ptr)() = __init_array_start[i];
if (ptr) ptr();
}
}

static void __libc_fini_array() {
ssize_t count, i;

count = __fini_array_end - __fini_array_start;
for (i = count - 1; i >= 0; i--)
__fini_array_start[i]();
for (i = count - 1; i >= 0; i--) {
void (*ptr)() = __fini_array_start[i];
if (ptr) ptr();
}
}

#define MAX_ATEXIT 32
Expand Down

0 comments on commit 69e0687

Please # to comment.