Skip to content

Commit 53424b9

Browse files
nvme: plot eye chart data and hex dumping VS eye data
-Addressing review comments -Added checks for memory leaks Signed-off-by: Sivaprasad Gutha <sivaprasad6541@gmail.com>
1 parent 75790b2 commit 53424b9

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

nvme-print-json.c

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,11 +2128,14 @@ static char *json_eom_printable_eye(struct nvme_eom_lane_desc *lane,
21282128
uint16_t ncols = le16_to_cpu(lane->ncols);
21292129
char *printable = NULL;
21302130
char *printable_start = NULL;
2131-
struct json_object *eye_array = json_create_array();
2131+
struct json_object *eye_array = NULL;
21322132

21332133
if (nrows == 0 || ncols == 0)
21342134
return NULL;
21352135

2136+
eye_array = json_create_array();
2137+
if (!eye_array)
2138+
return NULL;
21362139
/*
21372140
* Allocate buffer for full printable string (with newlines)
21382141
* +1 for null terminator
@@ -2141,21 +2144,13 @@ static char *json_eom_printable_eye(struct nvme_eom_lane_desc *lane,
21412144
printable_start = printable;
21422145

21432146
if (!printable)
2144-
return NULL;
2145-
2146-
if (!eye_array) {
2147-
free(printable);
2148-
return NULL;
2149-
}
2147+
goto fail_free_eye_array;
21502148

21512149
for (int i = 0; i < nrows; i++) {
21522150
char *row = malloc(ncols + 1);
21532151

2154-
if (!row) {
2155-
/* Cleanup on failure */
2156-
free(printable_start);
2157-
return NULL;
2158-
}
2152+
if (!row)
2153+
goto fail_free_eye_printable;
21592154

21602155
for (int j = 0; j < ncols; j++) {
21612156
char ch = eye[i * ncols + j];
@@ -2175,6 +2170,13 @@ static char *json_eom_printable_eye(struct nvme_eom_lane_desc *lane,
21752170
obj_add_array(r, "printable_eye", eye_array);
21762171

21772172
return printable_start;
2173+
2174+
fail_free_eye_printable:
2175+
free(printable);
2176+
fail_free_eye_array:
2177+
json_free_object(eye_array);
2178+
2179+
return NULL;
21782180
}
21792181

21802182
static void json_phy_rx_eom_descs(struct nvme_phy_rx_eom_log *log,
@@ -2189,12 +2191,20 @@ static void json_phy_rx_eom_descs(struct nvme_phy_rx_eom_log *log,
21892191

21902192
for (i = 0; i < num_descs; i++) {
21912193
struct nvme_eom_lane_desc *desc = p;
2194+
_cleanup_free_ char *hexstr = NULL;
21922195
unsigned char *vsdata = NULL;
21932196
unsigned int vsdataoffset = 0;
2194-
struct json_object *jdesc = json_create_object();
2195-
uint16_t nrows = le16_to_cpu(desc->nrows);
2196-
uint16_t ncols = le16_to_cpu(desc->ncols);
2197-
uint16_t edlen = le16_to_cpu(desc->edlen);
2197+
uint16_t nrows, ncols, edlen;
2198+
struct json_object *jdesc;
2199+
char *hexdata;
2200+
2201+
jdesc = json_create_object();
2202+
if (!desc)
2203+
return;
2204+
2205+
nrows = le16_to_cpu(desc->nrows);
2206+
ncols = le16_to_cpu(desc->ncols);
2207+
edlen = le16_to_cpu(desc->edlen);
21982208

21992209
obj_add_uint(jdesc, "lid", desc->mstatus);
22002210
obj_add_uint(jdesc, "lane", desc->lane);
@@ -2214,16 +2224,18 @@ static void json_phy_rx_eom_descs(struct nvme_phy_rx_eom_log *log,
22142224
continue;
22152225

22162226
/* 2 hex chars + space per byte */
2217-
_cleanup_free_ char *hexstr = malloc(edlen * 3 + 1);
2227+
hexstr = malloc(edlen * 3 + 1);
22182228

2219-
if (!hexstr)
2229+
if (!hexstr) {
2230+
json_free_object(jdesc);
22202231
return;
2232+
}
22212233

22222234
/* Hex dump Vendor Specific Eye Data */
22232235
vsdataoffset = (nrows * ncols) + sizeof(struct nvme_eom_lane_desc);
22242236
vsdata = (unsigned char *)((unsigned char *)desc + vsdataoffset);
22252237

2226-
char *hexdata = hexstr;
2238+
hexdata = hexstr;
22272239

22282240
for (int offset = 0; offset < edlen; offset++)
22292241
hexdata += sprintf(hexdata, "%02X ", vsdata[offset]);

nvme-print-stdout.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,11 @@ static void stdout_phy_rx_eom_descs(struct nvme_phy_rx_eom_log *log)
792792
struct nvme_eom_lane_desc *desc = p;
793793
unsigned char *vsdata = NULL;
794794
unsigned int vsdataoffset = 0;
795-
uint16_t nrows = le16_to_cpu(desc->nrows);
796-
uint16_t ncols = le16_to_cpu(desc->ncols);
797-
uint16_t edlen = le16_to_cpu(desc->edlen);
795+
uint16_t nrows, ncols, edlen;
796+
797+
nrows = le16_to_cpu(desc->nrows);
798+
ncols = le16_to_cpu(desc->ncols);
799+
edlen = le16_to_cpu(desc->edlen);
798800

799801
printf("Measurement Status: %s\n",
800802
desc->mstatus ? "Successful" : "Not Successful");
@@ -817,11 +819,15 @@ static void stdout_phy_rx_eom_descs(struct nvme_phy_rx_eom_log *log)
817819

818820
/* Hex dump Vendor Specific Eye Data */
819821
vsdata = malloc(edlen);
822+
if (!vsdata)
823+
return;
824+
820825
vsdataoffset = (nrows * ncols) + sizeof(struct nvme_eom_lane_desc);
821826
vsdata = (unsigned char *)((unsigned char *)desc + vsdataoffset);
822827
printf("Eye Data:\n");
823828
d(vsdata, edlen, 16, 1);
824829
printf("\n");
830+
free(vsdata);
825831

826832
p += log->dsize;
827833
}

0 commit comments

Comments
 (0)