]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
ocp: Fix print_formatted_var_size_str and json_add_formatted_var_size_str
authorjeff-lien-wdc <jeff.lien@sandisk.com>
Mon, 31 Mar 2025 21:54:33 +0000 (16:54 -0500)
committerDaniel Wagner <wagi@monom.org>
Wed, 2 Apr 2025 07:02:56 +0000 (07:02 +0000)
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 <jeff.lien@sandisk.com>
plugins/ocp/ocp-nvme.h
plugins/ocp/ocp-telemetry-decode.c
util/utils.c

index 4e697f2afca622a1a381ed87626c1e084cec9ff0..f56a5cb623cd315c14e769fd1490e52528c6e0e7 100644 (file)
@@ -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),
index 57375f58b964937d04b114b449a6d991aefe5e5c..80a27183c6e93730edfa1634465c3d84ed2da2ac 100644 (file)
@@ -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 */
 
index ea30a4b74f2c8bb19b37fea75b3987f13e47b704..6cd2fb80497a027cd3da7673fe3e0e21465f4fc9 100644 (file)
@@ -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)