From 2d4280b47a0445187e18752ce91e2d76fd05fd50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jere=20Lepp=C3=A4nen?= Date: Fri, 2 Sep 2022 10:17:01 +0300 Subject: helper: cli: check for vsnprintf errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If vsnprintf() fails, log an error and return error status. Signed-off-by: Jere Leppänen Reviewed-by: Tuomas Taipale --- helper/cli.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'helper') diff --git a/helper/cli.c b/helper/cli.c index a514086e2..6a0230c50 100644 --- a/helper/cli.c +++ b/helper/cli.c @@ -206,7 +206,7 @@ static int cli_log_va(odp_log_level_t level, const char *fmt, va_list in_args) (void)level; va_list args; - char *str, *p, *last; + char *str = NULL, *p, *last; int len; /* @@ -221,8 +221,15 @@ static int cli_log_va(odp_log_level_t level, const char *fmt, va_list in_args) * string after the last newline and use it the next time we're called. */ va_copy(args, in_args); - len = vsnprintf(NULL, 0, fmt, args) + 1; + len = vsnprintf(NULL, 0, fmt, args); va_end(args); + + if (len < 0) { + ODPH_ERR("vsnprintf failed\n"); + goto out; + } + + len++; str = malloc(len); if (!str) { @@ -231,8 +238,14 @@ static int cli_log_va(odp_log_level_t level, const char *fmt, va_list in_args) } va_copy(args, in_args); - vsnprintf(str, len, fmt, args); + len = vsnprintf(str, len, fmt, args); va_end(args); + + if (len < 0) { + ODPH_ERR("vsnprintf failed\n"); + goto out; + } + p = str; last = strrchr(p, '\n'); -- cgit v1.2.3