From d85cfb3784a10430cc17618645fa66ff3df08040 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 2 Oct 2004 18:32:31 +0000 Subject: [PATCH] added wxVaCopy() and use it to fix wxVsnprintf() crash (see bug 1017651) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29604 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/function.tex | 14 ++++++++++++ include/wx/defs.h | 44 ++++++++++++++++++++++++++++++++++++++ src/common/string.cpp | 7 +++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex index d1874e25c3..aee339bb42 100644 --- a/docs/latex/wx/function.tex +++ b/docs/latex/wx/function.tex @@ -237,6 +237,7 @@ the corresponding topic. \helpref{wxUnix2DosFilename}{wxunix2dosfilename}\\ \helpref{wxUnsetEnv}{wxunsetenv}\\ \helpref{wxUsleep}{wxusleep}\\ +\helpref{wxVaCopy}{wxvacopy}\\ \helpref{wxVsnprintf}{wxvsnprintf}\\ \helpref{wxWakeUpIdle}{wxwakeupidle}\\ \helpref{wxWriteResource}{wxwriteresource}\\ @@ -2977,6 +2978,19 @@ allows to define unsigned 64 bit compile time constants: \helpref{wxLL}{wxll}, \helpref{wxLongLong}{wxlonglong} +\membersection{wxVaCopy}\label{wxvacopy} + +\func{void}{wxVaCopy}{\param{va\_list }{argptrDst}, \param{va\_list}{argptrSrc}} + +This macro is the same as the standard C99 \texttt{va\_copy} for the compilers +which support it or its replacement for those that don't. It must be used to +preserve the value of a \texttt{va\_list} object if you need to use it after +passing it to another function because it can be modified by the latter. + +As with \texttt{va\_start}, each call to \texttt{wxVaCopy} must have a matching +\texttt{va\_end}. + + \membersection{::wxWriteResource}\label{wxwriteresource} \func{bool}{wxWriteResource}{\param{const wxString\& }{section}, \param{const wxString\& }{entry}, diff --git a/include/wx/defs.h b/include/wx/defs.h index 5a0ede9afa..59d8729291 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -323,6 +323,50 @@ typedef int wxWindowID; #endif #endif +/* provide replacement for C99 va_copy() if the compiler doesn't have it */ + +/* could be already defined by configure or the user */ +#ifndef wxVaCopy + /* if va_copy is a macro or configure detected that we have it, use it */ + #if defined(va_copy) || defined(HAVE_VA_COPY) + #define wxVaCopy va_copy + #else /* no va_copy, try to provide a replacement */ + /* + configure tries to determine whether va_list is an array or struct + type, but it may not be used under Windows, so deal with a few + special cases. + */ + + #ifdef __WATCOMC__ + /* Watcom uses array type for va_list except for PPC and Alpha */ + #if !defined(__PPC__) && !defined(__AXP__) + #define VA_LIST_IS_ARRAY + #endif + #endif /* __WATCOMC__ */ + + #if defined(__PPC__) && (defined(_CALL_SYSV) || defined (_WIN32)) + /* + PPC using SysV ABI and NT/PPC are special in that they use an + extra level of indirection. + */ + #define VA_LIST_IS_POINTER + #endif /* SysV or Win32 on __PPC__ */ + + /* + note that we use memmove(), not memcpy(), in case anybody tries + to do wxVaCopy(ap, ap) + */ + #if defined(VA_LIST_IS_POINTER) + #define wxVaCopy(d, s) memmove(*(d), *(s), sizeof(va_list)) + #elif defined(VA_LIST_IS_ARRAY) + #define wxVaCopy(d, s) memmove((d), (s), sizeof(va_list)) + #else /* we can only hope that va_lists are simple lvalues */ + #define wxVaCopy(d, s) ((d) = (s)) + #endif + #endif /* va_copy/!va_copy */ +#endif // wxVaCopy + + /* ---------------------------------------------------------------------------- */ /* portable calling conventions macros */ /* ---------------------------------------------------------------------------- */ diff --git a/src/common/string.cpp b/src/common/string.cpp index f42bd4dfd4..07a532d180 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1727,7 +1727,12 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) return -1; } - len = wxVsnprintf(buf, size, pszFormat, argptr); + // wxVsnprintf() may modify the original arg pointer, so pass it + // only a copy + va_list argptrcopy; + wxVaCopy(argptrcopy, argptr); + len = wxVsnprintf(buf, size, pszFormat, argptrcopy); + va_end(argptrcopy); // some implementations of vsnprintf() don't NUL terminate // the string if there is not enough space for it so -- 2.47.2