From d2a6ba63df4c4dbe218ebb383d81e35f0b43a110 Mon Sep 17 00:00:00 2001 From: DavidBar-On Date: Thu, 12 Sep 2024 14:45:28 +0300 Subject: [PATCH] Changes per reviewer comments (with rebase) --- src/iperf_api.c | 55 +++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index 2d8d4ed42..30fa13555 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -2800,35 +2800,40 @@ JSON_read(int fd) * structure, NULL if there was an error. */ rc = Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp); - hsize = ntohl(nsize); - if (rc == sizeof(nsize) && hsize > 0 && hsize <= MAX_PARAMS_JSON_STRING) { - /* Allocate a buffer to hold the JSON */ - strsize = hsize + 1; /* +1 for trailing NULL */ - if (strsize) { - str = (char *) calloc(sizeof(char), strsize); - if (str != NULL) { - rc = Nread(fd, str, hsize, Ptcp); - if (rc >= 0) { - /* - * We should be reading in the number of bytes corresponding to the - * length in that 4-byte integer. If we don't the socket might have - * prematurely closed. Only do the JSON parsing if we got the - * correct number of bytes. - */ - if (rc == hsize) { - json = cJSON_Parse(str); - } - else { - printf("WARNING: Size of data read does not correspond to offered length\n"); - } - } - } - free(str); + if (rc == sizeof(nsize)) { + hsize = ntohl(nsize); + if (hsize > 0 && hsize <= MAX_PARAMS_JSON_STRING) { + /* Allocate a buffer to hold the JSON */ + strsize = hsize + 1; /* +1 for trailing NULL */ + if (strsize) { + str = (char *) calloc(sizeof(char), strsize); + if (str != NULL) { + rc = Nread(fd, str, hsize, Ptcp); + if (rc >= 0) { + /* + * We should be reading in the number of bytes corresponding to the + * length in that 4-byte integer. If we don't the socket might have + * prematurely closed. Only do the JSON parsing if we got the + * correct number of bytes. + */ + if (rc == hsize) { + json = cJSON_Parse(str); + } + else { + printf("WARNING: JSON size of data read does not correspond to offered length\n"); + } + } + free(str); + } + } } else { - printf("WARNING: Data length overflow\n"); + printf("WARNING: JSON data length overflow\n"); } } + else { + printf("WARNING: Failed to read JSN data size\n"); + } return json; }