From 7a1603642cb04fd7b3a11c784f2b9cb39d27c625 Mon Sep 17 00:00:00 2001 From: jeff-lien-wdc Date: Mon, 31 Mar 2025 16:54:33 -0500 Subject: [PATCH] ocp: Fix print_formatted_var_size_str and json_add_formatted_var_size_str These 2 functions, used by the ocp plugin, had string arrays with hard coded lengths. In some cases, the hard coded length was not long enough for strings that were being printed. The functions were changed to allocate a buffers long enough to contain the full string. Updated the OCP plugin version to 2.12.0 Signed-off-by: jeff-lien-wdc --- plugins/ocp/ocp-nvme.h | 2 +- plugins/ocp/ocp-telemetry-decode.c | 6 +++++- util/utils.c | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/plugins/ocp/ocp-nvme.h b/plugins/ocp/ocp-nvme.h index 4e697f2a..f56a5cb6 100644 --- a/plugins/ocp/ocp-nvme.h +++ b/plugins/ocp/ocp-nvme.h @@ -11,7 +11,7 @@ #if !defined(OCP_NVME) || defined(CMD_HEADER_MULTI_READ) #define OCP_NVME -#define OCP_PLUGIN_VERSION "2.11.0" +#define OCP_PLUGIN_VERSION "2.12.0" #include "cmd.h" PLUGIN(NAME("ocp", "OCP cloud SSD extensions", OCP_PLUGIN_VERSION), diff --git a/plugins/ocp/ocp-telemetry-decode.c b/plugins/ocp/ocp-telemetry-decode.c index 57375f58..80a27183 100644 --- a/plugins/ocp/ocp-telemetry-decode.c +++ b/plugins/ocp/ocp-telemetry-decode.c @@ -464,15 +464,19 @@ void json_add_formatted_u32_str(struct json_object *pobject, const char *msg, un void json_add_formatted_var_size_str(struct json_object *pobject, const char *msg, __u8 *pdata, unsigned int data_size) { - char description_str[256] = ""; + char *description_str = NULL; char temp_buffer[3] = { 0 }; + /* Allocate 2 chars for each value in the data + 2 bytes for the null terminator */ + description_str = (char *) calloc(1, data_size*2 + 2); + for (size_t i = 0; i < data_size; ++i) { sprintf(temp_buffer, "%02X", pdata[i]); strcat(description_str, temp_buffer); } json_object_add_value_string(pobject, msg, description_str); + free(description_str); } #endif /* CONFIG_JSONC */ diff --git a/util/utils.c b/util/utils.c index ea30a4b7..6cd2fb80 100644 --- a/util/utils.c +++ b/util/utils.c @@ -138,9 +138,12 @@ unsigned char *read_binary_file(char *data_dir_path, const char *bin_path, void print_formatted_var_size_str(const char *msg, const __u8 *pdata, size_t data_size, FILE *fp) { - char description_str[1024] = ""; + char *description_str = NULL; char temp_buffer[3] = { 0 }; + /* Allocate 2 chars for each value in the data + 2 bytes for the null terminator */ + description_str = (char *) calloc(1, data_size*2 + 2); + for (size_t i = 0; i < data_size; ++i) { sprintf(temp_buffer, "%02X", pdata[i]); strcat(description_str, temp_buffer); @@ -150,6 +153,7 @@ void print_formatted_var_size_str(const char *msg, const __u8 *pdata, size_t dat fp = stdout; fprintf(fp, "%s: %s\n", msg, description_str); + free(description_str); } void process_field_size_16(int offset, char *sfield, __u8 *buf, char *datastr) -- 2.50.1