From 79630e87348fdde7db5c7cd0cf36214e30aa1f6a Mon Sep 17 00:00:00 2001 From: "Bruce A. Mah" Date: Tue, 19 May 2020 07:44:09 -0700 Subject: [PATCH] fix: Fix memory management issues around calls to cJSON_Print() (#997) Related to #883. --- src/iperf_api.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index a5dd6ae..935f616 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -1768,7 +1768,9 @@ send_parameters(struct iperf_test *test) cJSON_AddStringToObject(j, "client_version", IPERF_VERSION); if (test->debug) { - printf("send_parameters:\n%s\n", cJSON_Print(j)); + char *str = cJSON_Print(j); + printf("send_parameters:\n%s\n", str); + cJSON_free(str); } if (JSON_write(test->ctrl_sck, j) < 0) { @@ -1798,7 +1800,7 @@ get_parameters(struct iperf_test *test) char *str; str = cJSON_Print(j); printf("get_parameters:\n%s\n", str ); - free(str); + cJSON_free(str); } if ((j_p = cJSON_GetObjectItem(j, "tcp")) != NULL) @@ -1965,7 +1967,7 @@ send_results(struct iperf_test *test) if (r == 0 && test->debug) { char *str = cJSON_Print(j); printf("send_results\n%s\n", str); - free(str); + cJSON_free(str); } if (r == 0 && JSON_write(test->ctrl_sck, j) < 0) { i_errno = IESENDRESULTS; @@ -2023,7 +2025,7 @@ get_results(struct iperf_test *test) if (test->debug) { char *str = cJSON_Print(j); printf("get_results\n%s\n", str); - free(str); + cJSON_free(str); } test->remote_cpu_util[0] = j_cpu_util_total->valuedouble; @@ -2160,7 +2162,7 @@ JSON_write(int fd, cJSON *json) if (Nwrite(fd, str, hsize, Ptcp) < 0) r = -1; } - free(str); + cJSON_free(str); } return r; } @@ -3447,7 +3449,9 @@ iperf_print_results(struct iperf_test *test) /* Print server output if we're on the client and it was requested/provided */ if (test->role == 'c' && iperf_get_test_get_server_output(test) && !test->json_output) { if (test->json_server_output) { - iperf_printf(test, "\nServer JSON output:\n%s\n", cJSON_Print(test->json_server_output)); + char *str = cJSON_Print(test->json_server_output); + iperf_printf(test, "\nServer JSON output:\n%s\n", str); + cJSON_free(str); cJSON_Delete(test->json_server_output); test->json_server_output = NULL; } @@ -4031,6 +4035,8 @@ iperf_json_finish(struct iperf_test *test) return -1; fprintf(test->outfile, "%s\n", test->json_output_string); iflush(test); + cJSON_free(test->json_output_string); + test->json_output_string = NULL; cJSON_Delete(test->json_top); test->json_top = test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL; return 0;