diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/windows/guid_string.cc | 8 | ||||
-rw-r--r-- | src/common/windows/http_upload.cc | 4 | ||||
-rw-r--r-- | src/common/windows/pdb_source_line_writer.cc | 17 | ||||
-rw-r--r-- | src/common/windows/string_utils-inl.h | 32 |
4 files changed, 31 insertions, 30 deletions
diff --git a/src/common/windows/guid_string.cc b/src/common/windows/guid_string.cc index e77432a3..dcc22aa0 100644 --- a/src/common/windows/guid_string.cc +++ b/src/common/windows/guid_string.cc @@ -42,26 +42,30 @@ namespace google_breakpad { // static wstring GUIDString::GUIDToWString(GUID *guid) { wchar_t guid_string[37]; - WindowsStringUtils::safe_swprintf( + swprintf( guid_string, sizeof(guid_string) / sizeof(guid_string[0]), L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", guid->Data1, guid->Data2, guid->Data3, guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]); + GB_WSU_SAFE_SWPRINTF_TERMINATE(guid_string, + sizeof(guid_string) / sizeof(guid_string[0])); return wstring(guid_string); } // static wstring GUIDString::GUIDToSymbolServerWString(GUID *guid) { wchar_t guid_string[33]; - WindowsStringUtils::safe_swprintf( + swprintf( guid_string, sizeof(guid_string) / sizeof(guid_string[0]), L"%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X", guid->Data1, guid->Data2, guid->Data3, guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]); + GB_WSU_SAFE_SWPRINTF_TERMINATE(guid_string, + sizeof(guid_string) / sizeof(guid_string[0])); return wstring(guid_string); } diff --git a/src/common/windows/http_upload.cc b/src/common/windows/http_upload.cc index 5fe3b6dc..86ecf37a 100644 --- a/src/common/windows/http_upload.cc +++ b/src/common/windows/http_upload.cc @@ -213,8 +213,8 @@ wstring HTTPUpload::GenerateMultipartBoundary() { int r1 = rand(); wchar_t temp[kBoundaryLength]; - WindowsStringUtils::safe_swprintf(temp, kBoundaryLength, L"%s%08X%08X", - kBoundaryPrefix, r0, r1); + swprintf(temp, kBoundaryLength, L"%s%08X%08X", kBoundaryPrefix, r0, r1); + GB_WSU_SAFE_SWPRINTF_TERMINATE(temp, kBoundaryLength); return wstring(temp); } diff --git a/src/common/windows/pdb_source_line_writer.cc b/src/common/windows/pdb_source_line_writer.cc index 071940d6..9f4ae4b5 100644 --- a/src/common/windows/pdb_source_line_writer.cc +++ b/src/common/windows/pdb_source_line_writer.cc @@ -723,9 +723,10 @@ bool PDBSourceLineWriter::GetModuleInfo(PDBModuleInfo *info) { // Use the same format that the MS symbol server uses in filesystem // hierarchies. wchar_t age_string[9]; - WindowsStringUtils::safe_swprintf( - age_string, sizeof(age_string) / sizeof(age_string[0]), - L"%x", age); + swprintf(age_string, sizeof(age_string) / sizeof(age_string[0]), + L"%x", age); + GB_WSU_SAFE_SWPRINTF_TERMINATE(age_string, + sizeof(age_string) / sizeof(age_string[0])); info->debug_identifier = GUIDString::GUIDToSymbolServerWString(&guid); info->debug_identifier.append(age_string); @@ -738,10 +739,12 @@ bool PDBSourceLineWriter::GetModuleInfo(PDBModuleInfo *info) { // Use the same format that the MS symbol server uses in filesystem // hierarchies. wchar_t identifier_string[17]; - WindowsStringUtils::safe_swprintf( - identifier_string, - sizeof(identifier_string) / sizeof(identifier_string[0]), - L"%08X%x", signature, age); + swprintf(identifier_string, + sizeof(identifier_string) / sizeof(identifier_string[0]), + L"%08X%x", signature, age); + GB_WSU_SAFE_SWPRINTF_TERMINATE(identifier_string, + sizeof(identifier_string) / + sizeof(identifier_string[0])); info->debug_identifier = identifier_string; } diff --git a/src/common/windows/string_utils-inl.h b/src/common/windows/string_utils-inl.h index de6e2e25..fd7bc228 100644 --- a/src/common/windows/string_utils-inl.h +++ b/src/common/windows/string_utils-inl.h @@ -49,6 +49,19 @@ #define WIN_STRING_FORMAT_LL "I64" #endif // MSC_VER >= 1400 +// When using swprintf, call GB_WSU_SWPRINTF_TERMINATE afterwards using the +// first two arguments to swprintf. This will ensure that the buffer is +// 0-terminated. MSVC8's swprintf always 0-terminates the buffer, so the +// macro is a no-op. This is done in a macro rather than a function +// because the function approach relies on vswprintf, which is incompatible +// with some analysis tools. +#if _MSC_VER >= 1400 // MSVC 2005/8 +#define GB_WSU_SAFE_SWPRINTF_TERMINATE(buffer, count); +#else // _MSC_VER >= 1400 +#define GB_WSU_SAFE_SWPRINTF_TERMINATE(buffer, count); \ + (buffer)[(count) - 1] = L'\0'; +#endif // _MSC_VER >= 1400 + namespace google_breakpad { using std::string; @@ -56,10 +69,6 @@ using std::wstring; class WindowsStringUtils { public: - // Equivalent to MSVC8's swprintf, which always 0-terminates buffer. - static void safe_swprintf(wchar_t *buffer, size_t count, - const wchar_t *format, ...); - // Roughly equivalent to MSVC8's wcscpy_s, except pre-MSVC8, this does // not fail if source is longer than destination_size. The destination // buffer is always 0-terminated. @@ -90,21 +99,6 @@ class WindowsStringUtils { }; // static -inline void WindowsStringUtils::safe_swprintf(wchar_t *buffer, size_t count, - const wchar_t *format, ...) { - va_list args; - va_start(args, format); - vswprintf(buffer, count, format, args); - -#if _MSC_VER < 1400 // MSVC 2005/8 - // Pre-MSVC 2005/8 doesn't 0-terminate the buffer if the formatted string - // is larger than the buffer. Ensure that the string is 0-terminated. - if (buffer && count) - buffer[count - 1] = 0; -#endif // _MSC_VER < 1400 -} - -// static inline void WindowsStringUtils::safe_wcscpy(wchar_t *destination, size_t destination_size, const wchar_t *source) { |