handle realloc error better

Make sure we free the old pointer, clear it and set the array size to 0.
Use reallocarray where possible.
This commit is contained in:
Wim Taymans 2022-04-27 10:09:06 +02:00
parent 9e3b784b34
commit ba7d410c3c
10 changed files with 240 additions and 120 deletions

View file

@ -395,8 +395,12 @@ static int ensure_size(struct message *m, uint32_t size)
alloc = SPA_ROUND_UP_N(SPA_MAX(m->allocated + size, 4096u), 4096u);
diff = alloc - m->allocated;
if ((data = realloc(m->data, alloc)) == NULL)
if ((data = realloc(m->data, alloc)) == NULL) {
free(m->data);
m->stat->allocated -= m->allocated;
m->allocated = 0;
return -errno;
}
m->stat->allocated += diff;
m->stat->accumulated += diff;
m->data = data;