From ef0dd8e5173195cd5c980cff5714d7915156c08c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 14 Jun 2003 16:45:31 +0000 Subject: [PATCH] always NUL-terminate log messages, even if they're longer than buffer size git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21157 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/log.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/common/log.cpp b/src/common/log.cpp index 02b83693bf..06b2283ad4 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -119,13 +119,24 @@ static inline bool IsLoggingEnabled() // macros and not all compilers inline vararg functions. // ---------------------------------------------------------------------------- +// wrapper for wxVsnprintf(s_szBuf) which always NULL-terminates it +static inline void PrintfInLogBug(const wxChar *szFormat, va_list argptr) +{ + if ( wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr) < 0 ) + { + // must NUL-terminate it manually + s_szBuf[s_szBufSize - 1] = _T('\0'); + } + //else: NUL-terminated by vsnprintf() +} + // generic log function void wxVLogGeneric(wxLogLevel level, const wxChar *szFormat, va_list argptr) { if ( IsLoggingEnabled() ) { wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); - wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); + PrintfInLogBug(szFormat, argptr); wxLog::OnLog(level, s_szBuf, time(NULL)); } @@ -145,11 +156,12 @@ void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...) if ( IsLoggingEnabled() ) { \ wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ \ - wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); \ + PrintfInLogBug(szFormat, argptr); \ \ wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ } \ } \ + \ void wxLog##level(const wxChar *szFormat, ...) \ { \ va_list argptr; \ -- 2.47.2