From 710290012eec7d8e067eb366c966212ab8b0e955 Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Mon, 17 Apr 2023 23:01:45 +0900 Subject: [PATCH] nvme-print-json: Use vasprintf instead of vsnprintf to allocate string Signed-off-by: Tokunori Ikegami --- nvme-print-json.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/nvme-print-json.c b/nvme-print-json.c index 1df36a5d..00135357 100644 --- a/nvme-print-json.c +++ b/nvme-print-json.c @@ -2860,21 +2860,15 @@ void json_output_status(int status) void json_output_error(const char *msg, va_list ap) { struct json_object *root = json_create_object(); - int len = ERROR_MSG_LEN; - char *error = (char *)malloc(ERROR_MSG_LEN); + char *error; - if (!error) - return; - - len = vsnprintf(error, len, msg, ap) + 1; - if (len > ERROR_MSG_LEN) { - error = (char *)realloc(error, len); - if (!error) - return; - vsnprintf(error, len, msg, ap); - } + if (vasprintf(&error, msg, ap) < 0) + error = NULL; - json_object_add_value_string(root, "error", error); + if (error) + json_object_add_value_string(root, "error", error); + else + json_object_add_value_string(root, "error", "Could not allocate string"); json_output_object(root); -- 2.49.0