From 412a5ab4ef63cef2da2c73cac5431f1249fa66ee 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/branches/WX_2_4_BRANCH@21157 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 6 ++++++ src/common/log.cpp | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 4bad84aa87..e4fbe547da 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -175,10 +175,16 @@ OTHER CHANGES 2.4.2 ----- +wxBase: + +- always NUL-terminate the log messages + All: - wxRegEx::Compile() now calculates the number of groups correctly + + 2.4.1 ----- diff --git a/src/common/log.cpp b/src/common/log.cpp index 8c5c23be29..0f2860dcfa 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -114,13 +114,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)); } @@ -140,11 +151,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; \