From 9050d81252a1850889986e223bdabf38d360d906 Mon Sep 17 00:00:00 2001 From: David Bar-On <61089727+davidBar-On@users.noreply.github.com> Date: Mon, 27 Jan 2025 21:58:35 +0200 Subject: [PATCH] JSON_Read() warning messages enhancements (#1817) * JSON_Read() warning messages enhancements * Change sprintf() to snprintf() per reviewer comment --- src/iperf_api.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index 0a973bc66..89a204cff 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -2778,6 +2778,7 @@ JSON_read(int fd, int max_size) char *str; cJSON *json = NULL; int rc; + char msg_buf[WARN_STR_LEN * 2]; /* * Read a four-byte integer, which is the length of the JSON to follow. @@ -2805,19 +2806,27 @@ JSON_read(int fd, int max_size) json = cJSON_Parse(str); } else { - warning("JSON size of data read does not correspond to offered length"); + snprintf(msg_buf, sizeof(msg_buf), "JSON size of data read does not correspond to offered length - expected %d bytes but received %d; errno=%d", hsize, rc, errno); + warning(msg_buf); } } + else { + snprintf(msg_buf, sizeof(msg_buf), "JSON data read failed; errno=%d", errno); + warning(msg_buf); + } free(str); } } } else { - warning("JSON data length overflow"); + snprintf(msg_buf, sizeof(msg_buf), "JSON data length overflow - %d bytes JSON size is not allowed", hsize); + warning(msg_buf); } } else { warning("Failed to read JSON data size"); + snprintf(msg_buf, sizeof(msg_buf), "Failed to read JSON data size - read returned %d; errno=%d", rc, errno); + warning(msg_buf); } return json; }