From 191a0989a1f54b7a3b3b8d90de3bced748b74d49 Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Sun, 30 Mar 2025 21:55:14 +0900 Subject: [PATCH] nvme-print: use allocation error string variable Use the variable instead of the hardcorded and duplicated strings. Signed-off-by: Tokunori Ikegami --- nvme-print-json.c | 10 +++++----- nvme-print-stdout.c | 2 +- nvme-print.c | 2 ++ nvme-print.h | 2 ++ 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/nvme-print-json.c b/nvme-print-json.c index b7b198eb..775bbc00 100644 --- a/nvme-print-json.c +++ b/nvme-print-json.c @@ -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)); diff --git a/nvme-print-stdout.c b/nvme-print-stdout.c index 23d5ba9c..de649778 100644 --- a/nvme-print-stdout.c +++ b/nvme-print-stdout.c @@ -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); } diff --git a/nvme-print.c b/nvme-print.c index 824062a6..0685cba5 100644 --- a/nvme-print.c +++ b/nvme-print.c @@ -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; diff --git a/nvme-print.h b/nvme-print.h index ac3f9785..4d24d3e1 100644 --- a/nvme-print.h +++ b/nvme-print.h @@ -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 */ -- 2.50.1