fix list growing when inintial size is 0

This commit is contained in:
jacekpoz 2024-09-08 16:56:15 +02:00
parent 5f3362a929
commit af7e527ff2
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8

View file

@ -8,7 +8,9 @@
bool _grow_PtkList(void **data, uint32_t *allocated, size_t element_size) {
errno = 0;
void *tmp = realloc(*data, (*allocated * 2) * element_size);
const size_t new_allocated = *allocated == 0 ? 1 : *allocated * 2;
void *tmp = realloc(*data, new_allocated * element_size);
if (errno == ENOMEM || tmp == NULL) {
free(*data);
@ -16,7 +18,7 @@ bool _grow_PtkList(void **data, uint32_t *allocated, size_t element_size) {
}
*data = tmp;
*allocated *= 2;
*allocated = new_allocated;
return true;
}