From d2f39eb3587303cdeca3cc67f03f79717c182cf6 Mon Sep 17 00:00:00 2001 From: zvon Date: Thu, 16 Apr 2020 11:54:44 +0200 Subject: [PATCH] Fix possible nullptr error Turns out `vswprintf` doesn't behave like `sprintf` and doesn't return required buffer size if provided with NULL and 0 buffer length. It's weird that it worked so far. --- functions.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/functions.cpp b/functions.cpp index 42d3594..4b5570c 100644 --- a/functions.cpp +++ b/functions.cpp @@ -69,9 +69,10 @@ std::wstring LMsg( int id, ... ) { return local; va_start( args, id ); - int len = vswprintf( nullptr, 0, local, args ); + // _vscprintf doesn't count ending '\0' + int len = _vscprintf( local, args ) + 1; va_end( args ); - wchar_t *text = new wchar_t[len + 1]; + wchar_t *text = new wchar_t[len]; va_start( args, id ); vswprintf( text, len + 1, local, args ); std::wstring ret = text;