Introduce nalloc_nofail

This commit is contained in:
Andrea Mazzoleni 2026-07-06 18:38:50 +02:00
parent b297cbfa9b
commit db3f5af65a
12 changed files with 43 additions and 39 deletions

View file

@ -2211,14 +2211,17 @@ void* malloc_nofail(size_t size)
return ptr;
}
void* calloc_nofail(size_t count, size_t size)
void* nalloc_nofail(size_t count, size_t size)
{
void* ptr;
if (count > SIZE_MAX / size) {
log_fatal(EINTERNAL, "Allocation size overflow\n");
os_abort();
}
size *= count;
/* see the note in malloc_nofail() of why we don't use calloc() */
ptr = malloc(size ? size : 1);
void* ptr = malloc(size ? size : 1);
if (!ptr) {
/* LCOV_EXCL_START */
@ -2227,33 +2230,28 @@ void* calloc_nofail(size_t count, size_t size)
/* LCOV_EXCL_STOP */
}
memset(ptr, 0, size);
malloc_counter_inc(size);
return ptr;
}
void* calloc_nofail(size_t count, size_t size)
{
void* ptr = nalloc_nofail(count, size);
memset(ptr, 0, count * size);
return ptr;
}
char* strdup_nofail(const char* str)
{
size_t size;
char* ptr;
size_t size = strlen(str) + 1;
size = strlen(str) + 1;
ptr = malloc(size);
if (!ptr) {
/* LCOV_EXCL_START */
malloc_fail(size);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
char* ptr = malloc_nofail(size);
memcpy(ptr, str, size);
malloc_counter_inc(size);
return ptr;
}