]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-print: use allocation error string variable
authorTokunori Ikegami <ikegami.t@gmail.com>
Sun, 30 Mar 2025 12:55:14 +0000 (21:55 +0900)
committerDaniel Wagner <wagi@monom.org>
Wed, 2 Apr 2025 07:15:07 +0000 (07:15 +0000)
Use the variable instead of the hardcorded and duplicated strings.

Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
nvme-print-json.c
nvme-print-stdout.c
nvme-print.c
nvme-print.h

index b7b198eb60daeb581717f5a626e6ad764b2af059..775bbc0031e31130358f0145a51011dd07eaaefb 100644 (file)
@@ -119,7 +119,7 @@ static void obj_add_result(struct json_object *o, const char *v, ...)
        va_start(ap, v);
 
        if (vasprintf(&value, v, ap) < 0)
-               value = "Could not allocate string";
+               value = alloc_error;
 
        obj_add_str(o, "Result", value);
 
@@ -135,7 +135,7 @@ static void obj_add_key(struct json_object *o, const char *k, const char *v, ...
        va_start(ap, v);
 
        if (vasprintf(&value, v, ap) < 0)
-               value = "Could not allocate string";
+               value = alloc_error;
 
        obj_add_str(o, k, value);
 
@@ -4582,7 +4582,7 @@ static void json_output_error_status(int status, const char *msg, va_list ap)
        _cleanup_free_ char *value = NULL;
 
        if (vasprintf(&value, msg, ap) < 0)
-               value = "Could not allocate string";
+               value = alloc_error;
 
        sprintf(json_str, "Error: %s", value);
        r = obj_create(json_str);
@@ -4622,7 +4622,7 @@ static void json_output_message(bool error, const char *msg, va_list ap)
        _cleanup_free_ char *value = NULL;
 
        if (vasprintf(&value, msg, ap) < 0)
-               value = "Could not allocate string";
+               value = alloc_error;
 
        obj_add_str(r, error ? "error" : "result", value);
 
@@ -4636,7 +4636,7 @@ static void json_output_perror(const char *msg, va_list ap)
        _cleanup_free_ char *error = NULL;
 
        if (vasprintf(&error, msg, ap) < 0)
-               error = "Could not allocate string";
+               error = alloc_error;
 
        obj_add_key(r, "error", "%s: %s", error, strerror(errno));
 
index 23d5ba9c87814af06c003ff46fe169cf525cc9bd..de6497782ba3aa2d1a45c8026b5c2f7ccc8e789f 100644 (file)
@@ -5609,7 +5609,7 @@ static void stdout_perror(const char *msg, va_list ap)
        _cleanup_free_ char *error = NULL;
 
        if (vasprintf(&error, msg, ap) < 0)
-               error = "Could not allocate string";
+               error = alloc_error;
 
        perror(error);
 }
index 824062a6f998295b0cf0f2faa8e60edece846d0d..0685cba5d91a7fbdcd31af55556c933b4c81acfd 100644 (file)
@@ -26,6 +26,8 @@
 #define nvme_print_output_format(name, ...)                    \
        nvme_print(name, nvme_is_output_format_json() ? JSON : NORMAL, ##__VA_ARGS__);
 
+char *alloc_error = "Could not allocate string";
+
 static struct print_ops *nvme_print_ops(nvme_print_flags_t flags)
 {
        struct print_ops *ops = NULL;
index ac3f97856ea7be721a56a0020049a5017db4b996..4d24d3e1aa4dec08a6bfb6a6955d39f07f36b6f0 100644 (file)
@@ -352,4 +352,6 @@ void nvme_show_host_discovery_log(struct nvme_host_discover_log *log, nvme_print
 void nvme_show_ave_discovery_log(struct nvme_ave_discover_log *log, nvme_print_flags_t flags);
 void nvme_show_pull_model_ddc_req_log(struct nvme_pull_model_ddc_req_log *log,
                                      nvme_print_flags_t flags);
+
+extern char *alloc_error;
 #endif /* NVME_PRINT_H */