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

@ -156,17 +156,23 @@ static void *connection_ensure_size(struct pw_protocol_native_connection *conn,
int res;
if (buf->buffer_size + size > buf->buffer_maxsize) {
buf->buffer_maxsize = SPA_ROUND_UP_N(buf->buffer_size + size, MAX_BUFFER_SIZE);
buf->buffer_data = realloc(buf->buffer_data, buf->buffer_maxsize);
if (buf->buffer_data == NULL) {
void *np;
size_t ns;
ns = SPA_ROUND_UP_N(buf->buffer_size + size, MAX_BUFFER_SIZE);
np = realloc(buf->buffer_data, ns);
if (np == NULL) {
res = -errno;
free(buf->buffer_data);
buf->buffer_maxsize = 0;
spa_hook_list_call(&conn->listener_list,
struct pw_protocol_native_connection_events,
error, 0, -res);
error, 0, res);
errno = -res;
return NULL;
}
buf->buffer_maxsize = ns;
buf->buffer_data = np;
pw_log_debug("connection %p: resize buffer to %zd %zd %zd",
conn, buf->buffer_size, size, buf->buffer_maxsize);
}