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

@ -317,8 +317,15 @@ static int add_node_update(struct node_data *data, uint32_t change_mask, uint32_
res = spa_node_enum_params_sync(node->node,
id, &idx, NULL, &param, &b.b);
if (res == 1) {
params = realloc(params, sizeof(struct spa_pod *) * (n_params + 1));
params[n_params++] = spa_pod_copy(param);
void *p;
p = reallocarray(params, n_params + 1, sizeof(struct spa_pod *));
if (p == NULL) {
res = -errno;
pw_log_error("realloc failed: %m");
} else {
params = p;
params[n_params++] = spa_pod_copy(param);
}
}
spa_pod_dynamic_builder_clean(&b);
if (res != 1)
@ -376,8 +383,15 @@ static int add_port_update(struct node_data *data, struct pw_impl_port *port, ui
port->direction, port->port_id,
id, &idx, NULL, &param, &b.b);
if (res == 1) {
params = realloc(params, sizeof(struct spa_pod *) * (n_params + 1));
params[n_params++] = spa_pod_copy(param);
void *p;
p = reallocarray(params, n_params + 1, sizeof(struct spa_pod*));
if (p == NULL) {
res = -errno;
pw_log_error("realloc failed: %m");
} else {
params = p;
params[n_params++] = spa_pod_copy(param);
}
}
spa_pod_dynamic_builder_clean(&b);