From 851e65eceeeb4de11e24ccf72c5d6f5551736fda Mon Sep 17 00:00:00 2001 From: Philip Molloy Date: Mon, 1 Jul 2024 16:17:08 +0200 Subject: [PATCH] context: Handle if description is NULL and ctx->description exists 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 #1121 Signed-off-by: Philip Molloy --- context.c | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/context.c b/context.c index a202e80e9..2e9f8a5d0 100644 --- a/context.c +++ b/context.c @@ -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;