From 8573c3409e0c4552851825f3bc987df73531b691 Mon Sep 17 00:00:00 2001 From: Simon Arlott Date: Sun, 30 Jul 2017 17:03:30 +0100 Subject: [PATCH] librb: rb_snprinf_append: handle error return value from vsnprintf() --- librb/src/tools.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/librb/src/tools.c b/librb/src/tools.c index e576adcc..05c7e3eb 100644 --- a/librb/src/tools.c +++ b/librb/src/tools.c @@ -303,22 +303,25 @@ int rb_snprintf_append(char *str, size_t len, const char *format, ...) { if(len == 0) - return 0; + return -1; - size_t x = strlen(str); + int orig_len = strlen(str); - if(len < x) + if((int)len < orig_len) { str[len - 1] = '\0'; - return (int)len - 1; + return len - 1; } va_list ap; va_start(ap, format); - int y = (vsnprintf(str + x, len - x, format, ap) + (int)x); + int append_len = vsnprintf(str + orig_len, len - orig_len, format, ap); va_end(ap); - return (y); + if (append_len < 0) + return append_len; + + return (orig_len + append_len); } /* rb_basename