]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-print-json: Use vasprintf instead of vsnprintf to allocate string
authorTokunori Ikegami <ikegami.t@gmail.com>
Mon, 17 Apr 2023 14:01:45 +0000 (23:01 +0900)
committerDaniel Wagner <wagi@monom.org>
Mon, 17 Apr 2023 15:29:51 +0000 (17:29 +0200)
Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
nvme-print-json.c

index 1df36a5d916fada0cdddc5dab2817c4223cd90a7..0013535715e4c6451f6bb516af07978919dca180 100644 (file)
@@ -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);