From d794018606dfdb62f87adf065c5ba48a4607d92b Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Fri, 10 Apr 2015 14:43:15 -0700 Subject: Better fix for snprintf non-termination on windows. Replaces https://codereview.chromium.org/1062983002/ BUG=469244 R=brucedawson@chromium.org Review URL: https://codereview.chromium.org/1077903002 --- core/src/fxcrt/fx_basic_util.cpp | 18 ++++++++++++++++++ core/src/fxcrt/fx_basic_wstring.cpp | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) (limited to 'core/src/fxcrt') diff --git a/core/src/fxcrt/fx_basic_util.cpp b/core/src/fxcrt/fx_basic_util.cpp index 5a40c2b4be..c3df07fef5 100644 --- a/core/src/fxcrt/fx_basic_util.cpp +++ b/core/src/fxcrt/fx_basic_util.cpp @@ -169,6 +169,24 @@ FX_FLOAT FX_atof(FX_BSTR strc) } return bNegative ? -value : value; } + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900 +void FXSYS_snprintf(char *str, size_t size, _Printf_format_string_ const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + FXSYS_vsnprintf(str, size, fmt, ap); + va_end(ap); +} +void FXSYS_vsnprintf(char *str, size_t size, const char* fmt, va_list ap) +{ + (void) _vsnprintf(str, size, fmt, ap); + if (size) { + str[size - 1] = 0; + } +} +#endif // _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900 + static FX_BOOL FX_IsDigit(FX_BYTE ch) { return (ch >= '0' && ch <= '9') ? TRUE : FALSE; diff --git a/core/src/fxcrt/fx_basic_wstring.cpp b/core/src/fxcrt/fx_basic_wstring.cpp index ce6a1cd763..dfdbef8bd6 100644 --- a/core/src/fxcrt/fx_basic_wstring.cpp +++ b/core/src/fxcrt/fx_basic_wstring.cpp @@ -976,9 +976,9 @@ void CFX_WideString::FormatV(FX_LPCWSTR lpszFormat, va_list argList) nItemLen = nPrecision + nWidth + 128; } else { double f; - char pszTemp[256] = {0}; + char pszTemp[256]; f = va_arg(argList, double); - FXSYS_snprintf(pszTemp, sizeof(pszTemp) - 1, "%*.*f", nWidth, nPrecision + 6, f ); + FXSYS_snprintf(pszTemp, sizeof(pszTemp), "%*.*f", nWidth, nPrecision + 6, f ); nItemLen = (FX_STRSIZE)FXSYS_strlen(pszTemp); } break; -- cgit v1.2.3