pod: handle realloc failure

When realloc fails, the original pointer is untouched so store the
result of realloc somewhere else so that we don't cause a leak.
This commit is contained in:
Wim Taymans 2023-10-11 17:26:40 +02:00
parent a9659d9dce
commit 5102e4cb8d

View file

@ -23,14 +23,15 @@ static int spa_pod_dynamic_builder_overflow(void *data, uint32_t size)
struct spa_pod_dynamic_builder *d = (struct spa_pod_dynamic_builder*)data; struct spa_pod_dynamic_builder *d = (struct spa_pod_dynamic_builder*)data;
int32_t old_size = d->b.size; int32_t old_size = d->b.size;
int32_t new_size = SPA_ROUND_UP_N(size, d->extend); int32_t new_size = SPA_ROUND_UP_N(size, d->extend);
void *old_data = d->b.data; void *old_data = d->b.data, *new_data;
if (old_data == d->data) if (old_data == d->data)
d->b.data = NULL; d->b.data = NULL;
if ((d->b.data = realloc(d->b.data, new_size)) == NULL) if ((new_data = realloc(d->b.data, new_size)) == NULL)
return -errno; return -errno;
if (old_data == d->data && d->b.data != old_data && old_size > 0) if (old_data == d->data && new_data != old_data && old_size > 0)
memcpy(d->b.data, old_data, old_size); memcpy(new_data, old_data, old_size);
d->b.data = new_data;
d->b.size = new_size; d->b.size = new_size;
return 0; return 0;
} }