From 7bf6ebe55cff322f6927c3fe401bceb9abe280d9 Mon Sep 17 00:00:00 2001 From: "Bruce A. Mah" Date: Mon, 12 Apr 2021 13:12:11 -0700 Subject: [PATCH] Make sure we don't pass in a negative buffer size. In theory this check should always succeed, given the relative buffer sizes as currently coded. --- src/iperf_api.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index 00663f7fc..11d0b2126 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -4571,12 +4571,15 @@ iperf_printf(struct iperf_test *test, const char* format, ...) return r0; r += r0; } - va_start(argp, format); - r0 = vsnprintf(linebuffer + r, sizeof(linebuffer) - r, format, argp); - va_end(argp); - if (r0 < 0) - return r0; - r += r0; + /* Should always be true as long as sizeof(ct) < sizeof(linebuffer) */ + if (r < sizeof(linebuffer)) { + va_start(argp, format); + r0 = vsnprintf(linebuffer + r, sizeof(linebuffer) - r, format, argp); + va_end(argp); + if (r0 < 0) + return r0; + r += r0; + } fprintf(test->outfile, "%s", linebuffer); if (test->role == 's' && iperf_get_test_get_server_output(test)) {