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

@ -76,6 +76,11 @@ struct mix {
struct buffer buffers[MAX_BUFFERS];
};
struct params {
uint32_t n_params;
struct spa_pod **params;
};
struct port {
struct pw_impl_port *port;
struct node *node;
@ -89,8 +94,7 @@ struct port {
struct spa_port_info info;
struct pw_properties *properties;
uint32_t n_params;
struct spa_pod **params;
struct params params;
unsigned int removed:1;
unsigned int destroyed:1;
@ -120,8 +124,7 @@ struct node {
struct port dummy;
uint32_t n_params;
struct spa_pod **params;
struct params params;
};
struct impl {
@ -177,6 +180,32 @@ struct impl {
#define pw_client_node_resource_port_set_mix_info(r,...) \
pw_client_node_resource(r,port_set_mix_info,1,__VA_ARGS__)
static int update_params(struct params *p, uint32_t n_params, const struct spa_pod **params)
{
uint32_t i;
for (i = 0; i < p->n_params; i++)
free(p->params[i]);
p->n_params = n_params;
if (p->n_params == 0) {
free(p->params);
p->params = NULL;
} else {
struct spa_pod **np;
np = reallocarray(p->params, p->n_params, sizeof(struct spa_pod *));
if (np == NULL) {
pw_log_error("%p: can't realloc: %m", p);
free(p->params);
p->params = NULL;
p->n_params = 0;
return -errno;
}
p->params = np;
}
for (i = 0; i < p->n_params; i++)
p->params[i] = params[i] ? spa_pod_copy(params[i]) : NULL;
return 0;
}
static int
do_port_use_buffers(struct impl *impl,
enum spa_direction direction,
@ -309,10 +338,10 @@ static int impl_node_enum_params(void *object, int seq,
struct spa_pod *param;
result.index = result.next++;
if (result.index >= this->n_params)
if (result.index >= this->params.n_params)
break;
param = this->params[result.index];
param = this->params.params[result.index];
if (param == NULL || !spa_pod_is_object_id(param, id))
continue;
@ -472,17 +501,9 @@ do_update_port(struct node *this,
const struct spa_pod **params,
const struct spa_port_info *info)
{
uint32_t i;
if (change_mask & PW_CLIENT_NODE_PORT_UPDATE_PARAMS) {
spa_log_debug(this->log, "%p: port %u update %d params", this, port->id, n_params);
for (i = 0; i < port->n_params; i++)
free(port->params[i]);
port->n_params = n_params;
port->params = realloc(port->params, port->n_params * sizeof(struct spa_pod *));
for (i = 0; i < port->n_params; i++) {
port->params[i] = params[i] ? spa_pod_copy(params[i]) : NULL;
}
update_params(&port->params, n_params, params);
}
if (change_mask & PW_CLIENT_NODE_PORT_UPDATE_INFO) {
@ -577,7 +598,7 @@ impl_node_port_enum_params(void *object, int seq,
spa_return_val_if_fail(port != NULL, -EINVAL);
pw_log_debug("%p: seq:%d port %d.%d id:%u start:%u num:%u n_params:%d",
this, seq, direction, port_id, id, start, num, port->n_params);
this, seq, direction, port_id, id, start, num, port->params.n_params);
result.id = id;
result.next = 0;
@ -586,10 +607,10 @@ impl_node_port_enum_params(void *object, int seq,
struct spa_pod *param;
result.index = result.next++;
if (result.index >= port->n_params)
if (result.index >= port->params.n_params)
break;
param = port->params[result.index];
param = port->params.params[result.index];
if (param == NULL || !spa_pod_is_object_id(param, id))
continue;
@ -951,16 +972,8 @@ client_node_update(void *data,
struct node *this = &impl->node;
if (change_mask & PW_CLIENT_NODE_UPDATE_PARAMS) {
uint32_t i;
pw_log_debug("%p: update %d params", this, n_params);
for (i = 0; i < this->n_params; i++)
free(this->params[i]);
this->n_params = n_params;
this->params = realloc(this->params, this->n_params * sizeof(struct spa_pod *));
for (i = 0; i < this->n_params; i++)
this->params[i] = params[i] ? spa_pod_copy(params[i]) : NULL;
update_params(&this->params, n_params, params);
}
if (change_mask & PW_CLIENT_NODE_UPDATE_INFO) {
spa_node_emit_info(&this->hooks, info);
@ -1186,12 +1199,7 @@ node_init(struct node *this,
static int node_clear(struct node *this)
{
uint32_t i;
for (i = 0; i < this->n_params; i++)
free(this->params[i]);
free(this->params);
update_params(&this->params, 0, NULL);
return 0;
}

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);

View file

@ -471,8 +471,19 @@ do_update_port(struct node *this,
for (i = 0; i < port->n_params; i++)
free(port->params[i]);
port->n_params = n_params;
port->params = realloc(port->params, port->n_params * sizeof(struct spa_pod *));
if (port->n_params == 0) {
free(port->params);
port->params = NULL;
} else {
void *p;
p = reallocarray(port->params, port->n_params, sizeof(struct spa_pod *));
if (p == NULL) {
pw_log_error("%p: port %u can't realloc: %m", this, port_id);
free(port->params);
port->n_params = 0;
}
port->params = p;
}
for (i = 0; i < port->n_params; i++) {
port->params[i] = params[i] ?
pw_protocol_native0_pod_from_v2(this->resource->client, params[i]) : NULL;
@ -1033,8 +1044,19 @@ client_node0_update(void *data,
for (i = 0; i < this->n_params; i++)
free(this->params[i]);
this->n_params = n_params;
this->params = realloc(this->params, this->n_params * sizeof(struct spa_pod *));
if (this->n_params == 0) {
free(this->params);
this->params = NULL;
} else {
void *p;
p = reallocarray(this->params, this->n_params, sizeof(struct spa_pod *));
if (p == NULL) {
pw_log_error("%p: can't realloc: %m", this);
free(this->params);
this->n_params = 0;
}
this->params = p;
}
for (i = 0; i < this->n_params; i++)
this->params[i] = params[i] ? spa_pod_copy(params[i]) : NULL;
}