Skip to content

Commit 7a16036

Browse files
jeff-lien-sndkigaw
authored andcommitted
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 <[email protected]>
1 parent bdc0e90 commit 7a16036

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

plugins/ocp/ocp-nvme.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#if !defined(OCP_NVME) || defined(CMD_HEADER_MULTI_READ)
1212
#define OCP_NVME
1313

14-
#define OCP_PLUGIN_VERSION "2.11.0"
14+
#define OCP_PLUGIN_VERSION "2.12.0"
1515
#include "cmd.h"
1616

1717
PLUGIN(NAME("ocp", "OCP cloud SSD extensions", OCP_PLUGIN_VERSION),

plugins/ocp/ocp-telemetry-decode.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,19 @@ void json_add_formatted_u32_str(struct json_object *pobject, const char *msg, un
464464
void json_add_formatted_var_size_str(struct json_object *pobject, const char *msg, __u8 *pdata,
465465
unsigned int data_size)
466466
{
467-
char description_str[256] = "";
467+
char *description_str = NULL;
468468
char temp_buffer[3] = { 0 };
469469

470+
/* Allocate 2 chars for each value in the data + 2 bytes for the null terminator */
471+
description_str = (char *) calloc(1, data_size*2 + 2);
472+
470473
for (size_t i = 0; i < data_size; ++i) {
471474
sprintf(temp_buffer, "%02X", pdata[i]);
472475
strcat(description_str, temp_buffer);
473476
}
474477

475478
json_object_add_value_string(pobject, msg, description_str);
479+
free(description_str);
476480
}
477481
#endif /* CONFIG_JSONC */
478482

util/utils.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ unsigned char *read_binary_file(char *data_dir_path, const char *bin_path,
138138

139139
void print_formatted_var_size_str(const char *msg, const __u8 *pdata, size_t data_size, FILE *fp)
140140
{
141-
char description_str[1024] = "";
141+
char *description_str = NULL;
142142
char temp_buffer[3] = { 0 };
143143

144+
/* Allocate 2 chars for each value in the data + 2 bytes for the null terminator */
145+
description_str = (char *) calloc(1, data_size*2 + 2);
146+
144147
for (size_t i = 0; i < data_size; ++i) {
145148
sprintf(temp_buffer, "%02X", pdata[i]);
146149
strcat(description_str, temp_buffer);
@@ -150,6 +153,7 @@ void print_formatted_var_size_str(const char *msg, const __u8 *pdata, size_t dat
150153
fp = stdout;
151154

152155
fprintf(fp, "%s: %s\n", msg, description_str);
156+
free(description_str);
153157
}
154158

155159
void process_field_size_16(int offset, char *sfield, __u8 *buf, char *datastr)

0 commit comments

Comments
 (0)