From 18be9305070a489036168f15b26a15e1ca520e79 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Wed, 18 Jul 2018 21:41:52 +0200 Subject: [PATCH] Fix bug in case of error returned by vsnprintf: the buffer may have been corrupted in case of error. Clean it before returning. --- m-string.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/m-string.h b/m-string.h index 8ad61255..4038729d 100644 --- a/m-string.h +++ b/m-string.h @@ -629,9 +629,14 @@ string_cat_printf (string_t v, const char format[], ...) va_end (args); va_start (args, format); size = vsnprintf (&v->ptr[v->size], v->alloc - v->size, format, args); + assert (size >= 0); } if (size >= 0) { v->size += (size_t) size; + } else if (v->ptr != NULL) { + // vsnprintf may have output some characters before returning an error. + // Undo this to have a clean state + v->ptr[v->size] = 0; } va_end (args); STRINGI_CONTRACT (v);