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

@ -202,19 +202,26 @@ int endpoint_update(struct endpoint *this,
{
if (change_mask & PW_CLIENT_ENDPOINT_UPDATE_PARAMS) {
uint32_t i;
size_t size = n_params * sizeof(struct spa_pod *);
pw_log_debug(NAME" %p: update %d params", this, n_params);
for (i = 0; i < this->n_params; i++)
free(this->params[i]);
this->params = realloc(this->params, size);
if (size > 0 && !this->params) {
this->n_params = 0;
goto no_mem;
}
this->n_params = n_params;
if (this->n_params == 0) {
free(this->params);
this->params = NULL;
} else {
void *p;
p = reallocarray(this->params, n_params, sizeof(struct spa_pod*));
if (p == NULL) {
free(this->params);
this->params = NULL;
this->n_params = 0;
goto no_mem;
}
this->params = p;
}
for (i = 0; i < this->n_params; i++) {
this->params[i] = params[i] ? spa_pod_copy(params[i]) : NULL;
endpoint_notify_subscribed(this, i, i+1);
@ -232,16 +239,22 @@ int endpoint_update(struct endpoint *this,
pw_properties_update(this->props, info->props);
if (info->change_mask & PW_ENDPOINT_CHANGE_MASK_PARAMS) {
size_t size = info->n_params * sizeof(struct spa_param_info);
this->info.params = realloc(this->info.params, size);
if (size > 0 && !this->info.params) {
this->info.n_params = 0;
goto no_mem;
}
this->info.n_params = info->n_params;
memcpy(this->info.params, info->params, size);
if (info->n_params == 0) {
free(this->info.params);
this->info.params = NULL;
} else {
void *p;
p = reallocarray(this->info.params, info->n_params, sizeof(struct spa_param_info));
if (p == NULL) {
free(this->info.params);
this->info.params = NULL;
this->info.n_params = 0;
goto no_mem;
}
this->info.params = p;
memcpy(this->info.params, info->params, info->n_params * sizeof(struct spa_param_info));
}
}
if (!this->info.name) {