Skip to content

Commit

Permalink
context: Handle if description is NULL and ctx->description exists
Browse files Browse the repository at this point in the history
Add check when attempting to concatenate description strings.

When description was NULL and ctx->description was a valid string then
ctx->description was freed and replaced by NULL. This caused iio_info to
not print the backend description string.

Additionally, do not free(NULL) when ctx->description is NULL.

Fixes analogdevicesinc#1121

Signed-off-by: Philip Molloy <[email protected]>
  • Loading branch information
pamolloy committed Jul 1, 2024
1 parent fea2a83 commit 851e65e
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,35 +614,40 @@ iio_create_context_from_xml(const struct iio_context_params *params,
}
}

if (description && ctx->description) {
len = iio_snprintf(NULL, 0, "%s %s",
ctx->description, description);
if (len < 0) {
ret = (int) len;
prm_perror(params, ret, "Unable to set context description");
goto err_context_destroy;
}
if (description) {
if (ctx->description) {
len = iio_snprintf(NULL, 0, "%s %s", ctx->description,
description);
if (len < 0) {
ret = (int) len;
prm_perror(params, ret,
"Unable to set context description");
goto err_context_destroy;
}

new_description = malloc(len + 1);
if (!new_description) {
ret = -ENOMEM;
prm_err(params, "Unable to alloc memory\n");
goto err_context_destroy;
}
new_description = malloc(len + 1);
if (!new_description) {
ret = -ENOMEM;
prm_err(params, "Unable to alloc memory\n");
goto err_context_destroy;
}

iio_snprintf(new_description, len + 1, "%s %s",
ctx->description, description);
} else if (description) {
new_description = iio_strdup(description);
if (!new_description) {
ret = -ENOMEM;
prm_err(params, "Unable to alloc memory\n");
goto err_context_destroy;
iio_snprintf(new_description, len + 1, "%s %s",
ctx->description, description);

free(ctx->description);
} else {
new_description = iio_strdup(description);
if (!new_description) {
ret = -ENOMEM;
prm_err(params, "Unable to alloc memory\n");
goto err_context_destroy;
}
}

ctx->description = new_description;
}

free(ctx->description);
ctx->description = new_description;
ctx->params = *params;

return ctx;
Expand Down

0 comments on commit 851e65e

Please sign in to comment.