From 73cd14b4af906e77f3d8b019962fb9979ff12620 Mon Sep 17 00:00:00 2001
From: mmentovai <mmentovai@4c0a9323-5329-0410-9bdc-e9ce6186880e>
Date: Wed, 15 Nov 2006 22:24:42 +0000
Subject: Airbag client libraries should compile under MSVC .NET 2003/7.1
 (#64). r=bryner

http://groups.google.com/group/airbag-dev/browse_thread/thread/b838faeb50f71818


git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@64 4c0a9323-5329-0410-9bdc-e9ce6186880e
---
 src/client/windows/handler/exception_handler.cc    |   7 +-
 .../windows/handler/exception_handler.vcproj       |   4 +
 src/common/windows/guid_string.cc                  |  15 ++-
 src/common/windows/http_upload.cc                  |  13 +-
 src/common/windows/pdb_source_line_writer.cc       |  19 ++-
 src/common/windows/string_utils-inl.h              | 132 +++++++++++++++++++++
 src/tools/windows/dump_syms/dump_syms.cc           |  12 +-
 src/tools/windows/dump_syms/dump_syms.vcproj       |   4 +
 src/tools/windows/symupload/symupload.cc           |  28 +++--
 src/tools/windows/symupload/symupload.vcproj       |   4 +
 10 files changed, 207 insertions(+), 31 deletions(-)
 create mode 100644 src/common/windows/string_utils-inl.h

(limited to 'src')

diff --git a/src/client/windows/handler/exception_handler.cc b/src/client/windows/handler/exception_handler.cc
index 761f3151..130add2e 100644
--- a/src/client/windows/handler/exception_handler.cc
+++ b/src/client/windows/handler/exception_handler.cc
@@ -31,6 +31,8 @@
 
 #include <cstdio>
 
+#include "common/windows/string_utils-inl.h"
+
 #include "client/windows/handler/exception_handler.h"
 #include "common/windows/guid_string.h"
 #include "google_airbag/common/minidump_format.h"
@@ -172,8 +174,9 @@ bool ExceptionHandler::WriteMinidump(const wstring &dump_path,
 bool ExceptionHandler::WriteMinidumpWithException(DWORD requesting_thread_id,
                                                   EXCEPTION_POINTERS *exinfo) {
   wchar_t dump_file_name[MAX_PATH];
-  swprintf_s(dump_file_name, MAX_PATH, L"%s\\%s.dmp",
-             dump_path_.c_str(), next_minidump_id_.c_str());
+  WindowsStringUtils::safe_swprintf(dump_file_name, MAX_PATH, L"%s\\%s.dmp",
+                                    dump_path_.c_str(),
+                                    next_minidump_id_.c_str());
 
   bool success = false;
   if (minidump_write_dump_) {
diff --git a/src/client/windows/handler/exception_handler.vcproj b/src/client/windows/handler/exception_handler.vcproj
index 458cac4b..d76aca23 100644
--- a/src/client/windows/handler/exception_handler.vcproj
+++ b/src/client/windows/handler/exception_handler.vcproj
@@ -298,6 +298,10 @@
 				RelativePath="..\..\..\google_airbag\common\minidump_format.h"
 				>
 			</File>
+			<File
+				RelativePath="..\..\..\common\windows\string_utils-inl.h"
+				>
+			</File>
 		</Filter>
 		<Filter
 			Name="Resource Files"
diff --git a/src/common/windows/guid_string.cc b/src/common/windows/guid_string.cc
index 86d5f736..42a0b21a 100644
--- a/src/common/windows/guid_string.cc
+++ b/src/common/windows/guid_string.cc
@@ -33,6 +33,8 @@
 
 #include <wchar.h>
 
+#include "common/windows/string_utils-inl.h"
+
 #include "common/windows/guid_string.h"
 
 namespace google_airbag {
@@ -40,12 +42,13 @@ namespace google_airbag {
 // static
 wstring GUIDString::GUIDToWString(GUID *guid) {
   wchar_t guid_string[37];
-  _snwprintf_s(guid_string, sizeof(guid_string) / sizeof(wchar_t), _TRUNCATE,
-               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]);
+  WindowsStringUtils::safe_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]);
   return wstring(guid_string);
 }
 
diff --git a/src/common/windows/http_upload.cc b/src/common/windows/http_upload.cc
index 6176bff8..8e5328b5 100644
--- a/src/common/windows/http_upload.cc
+++ b/src/common/windows/http_upload.cc
@@ -36,6 +36,8 @@
 
 #include <fstream>
 
+#include "common/windows/string_utils-inl.h"
+
 #include "common/windows/http_upload.h"
 
 namespace google_airbag {
@@ -166,7 +168,8 @@ wstring HTTPUpload::GenerateMultipartBoundary() {
   int r1 = rand();
 
   wchar_t temp[kBoundaryLength];
-  swprintf_s(temp, kBoundaryLength, L"%s%08X%08X", kBoundaryPrefix, r0, r1);
+  WindowsStringUtils::safe_swprintf(temp, kBoundaryLength, L"%s%08X%08X",
+                                    kBoundaryPrefix, r0, r1);
   return wstring(temp);
 }
 
@@ -232,8 +235,16 @@ bool HTTPUpload::GenerateRequestBody(const map<wstring, wstring> &parameters,
 // static
 void HTTPUpload::GetFileContents(const wstring &filename,
                                  vector<char> *contents) {
+  // The "open" method on pre-MSVC8 ifstream implementations doesn't accept a
+  // wchar_t* filename, so use _wfopen directly in that case.  For VC8 and
+  // later, _wfopen has been deprecated in favor of _wfopen_s, which does
+  // not exist in earlier versions, so let the ifstream open the file itself.
+#if _MSC_VER >= 1400  // MSVC 2005/8
   ifstream file;
   file.open(filename.c_str(), ios::binary);
+#else  // _MSC_VER >= 1400
+  ifstream file(_wfopen(filename.c_str(), L"rb"));
+#endif  // _MSC_VER >= 1400
   if (file.is_open()) {
     file.seekg(0, ios::end);
     int length = file.tellg();
diff --git a/src/common/windows/pdb_source_line_writer.cc b/src/common/windows/pdb_source_line_writer.cc
index 58588089..c6c1d5c4 100644
--- a/src/common/windows/pdb_source_line_writer.cc
+++ b/src/common/windows/pdb_source_line_writer.cc
@@ -32,6 +32,8 @@
 #include <dia2.h>
 #include <stdio.h>
 
+#include "common/windows/string_utils-inl.h"
+
 #include "common/windows/pdb_source_line_writer.h"
 #include "common/windows/guid_string.h"
 
@@ -161,7 +163,7 @@ bool PDBSourceLineWriter::PrintFunction(IDiaSymbol *function) {
     stack_param_size = GetFunctionStackParamSize(function);
   }
 
-  fprintf(output_, "FUNC %x %llx %x %ws\n",
+  fprintf(output_, "FUNC %x %" WIN_STRING_FORMAT_LL "x %x %ws\n",
           rva, length, stack_param_size, name);
 
   CComPtr<IDiaEnumLineNumbers> lines;
@@ -461,6 +463,11 @@ bool PDBSourceLineWriter::GetSymbolFunctionName(IDiaSymbol *function,
     // If a name comes from get_name because no undecorated form existed,
     // it's already formatted properly to be used as output.  Don't do any
     // additional processing.
+    //
+    // MSVC7's DIA seems to not undecorate names in as many cases as MSVC8's.
+    // This will result in calling get_name for some C++ symbols, so
+    // all of the parameter and return type information may not be included in
+    // the name string.
   } else {
     // C++ uses a bogus "void" argument for functions and methods that don't
     // take any parameters.  Take it out of the undecorated name because it's
@@ -472,7 +479,8 @@ bool PDBSourceLineWriter::GetSymbolFunctionName(IDiaSymbol *function,
     if (length >= replace_length) {
       wchar_t *name_end = *name + length - replace_length;
       if (wcscmp(name_end, replace_string) == 0) {
-        wcscpy_s(name_end, replace_length, replacement_string);
+        WindowsStringUtils::safe_wcscpy(name_end, replace_length,
+                                        replacement_string);
         length = wcslen(*name);
       }
     }
@@ -501,13 +509,14 @@ bool PDBSourceLineWriter::GetSymbolFunctionName(IDiaSymbol *function,
 
         // Undecorate the name by moving it one character to the left in its
         // buffer, and terminating it where the last '@' had been.
-        wcsncpy_s(*name, length, *name + 1, last_at - *name - 1);
-      } else if (*name[0] == '_') {
+        WindowsStringUtils::safe_wcsncpy(*name, length,
+                                         *name + 1, last_at - *name - 1);
+     } else if (*name[0] == '_') {
         // This symbol's name is encoded according to the cdecl rules.  The
         // name doesn't end in a '@' character followed by a decimal positive
         // integer, so it's not a stdcall name.  Strip off the leading
         // underscore.
-        wcsncpy_s(*name, length, *name + 1, length - 1);
+        WindowsStringUtils::safe_wcsncpy(*name, length, *name + 1, length);
       }
     }
   }
diff --git a/src/common/windows/string_utils-inl.h b/src/common/windows/string_utils-inl.h
new file mode 100644
index 00000000..62ce7ac3
--- /dev/null
+++ b/src/common/windows/string_utils-inl.h
@@ -0,0 +1,132 @@
+// Copyright (c) 2006, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//     * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// string_utils-inl.h: Safer string manipulation on Windows, supporting
+// pre-MSVC8 environments.
+
+#ifndef COMMON_WINDOWS_STRING_UTILS_INL_H__
+#define COMMON_WINDOWS_STRING_UTILS_INL_H__
+
+#include <stdarg.h>
+#include <wchar.h>
+
+// The "ll" printf format size specifier corresponding to |long long| was
+// intrudced in MSVC8.  Earlier versions did not provide this size specifier,
+// but "I64" can be used to print 64-bit types.  Don't use "I64" where "ll"
+// is available, in the event of oddball systems where |long long| is not
+// 64 bits wide.
+#if _MSC_VER >= 1400  // MSVC 2005/8
+#define WIN_STRING_FORMAT_LL "ll"
+#else  // MSC_VER >= 1400
+#define WIN_STRING_FORMAT_LL "I64"
+#endif  // MSC_VER >= 1400
+
+namespace google_airbag {
+
+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.
+  static void safe_wcscpy(wchar_t *destination, size_t destination_size,
+                          const wchar_t *source);
+
+  // Roughly equivalent to MSVC8's wcsncpy_s, except that _TRUNCATE cannot
+  // be passed directly, and pre-MSVC8, this will not fail if source or count
+  // are longer than destination_size.  The destination buffer is always
+  // 0-terminated.
+  static void safe_wcsncpy(wchar_t *destination, size_t destination_size,
+                           const wchar_t *source, size_t count);
+
+ private:
+  // Disallow instantiation and other object-based operations.
+  WindowsStringUtils();
+  WindowsStringUtils(const WindowsStringUtils&);
+  ~WindowsStringUtils();
+  void operator=(const 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) {
+#if _MSC_VER >= 1400  // MSVC 2005/8
+  wcscpy_s(destination, destination_size, source);
+#else  // _MSC_VER >= 1400
+  // Pre-MSVC 2005/8 doesn't have wcscpy_s.  Simulate it with wcsncpy.
+  // wcsncpy doesn't 0-terminate the destination buffer if the source string
+  // is longer than size.  Ensure that the destination is 0-terminated.
+  wcsncpy(destination, source, destination_size);
+  if (destination && destination_size)
+    destination[destination_size - 1] = 0;
+#endif  // _MSC_VER >= 1400
+}
+
+// static
+inline void WindowsStringUtils::safe_wcsncpy(wchar_t *destination,
+                                             size_t destination_size,
+                                             const wchar_t *source,
+                                             size_t count) {
+#if _MSC_VER >= 1400  // MSVC 2005/8
+  wcsncpy_s(destination, destination_size, source, count);
+#else  // _MSC_VER >= 1400
+  // Pre-MSVC 2005/8 doesn't have wcsncpy_s.  Simulate it with wcsncpy.
+  // wcsncpy doesn't 0-terminate the destination buffer if the source string
+  // is longer than size.  Ensure that the destination is 0-terminated.
+  if (destination_size < count)
+    count = destination_size;
+
+  wcsncpy(destination, source, count);
+  if (destination && count)
+    destination[count - 1] = 0;
+#endif  // _MSC_VER >= 1400
+}
+
+}  // namespace google_airbag
+
+#endif  // COMMON_WINDOWS_STRING_UTILS_INL_H__
diff --git a/src/tools/windows/dump_syms/dump_syms.cc b/src/tools/windows/dump_syms/dump_syms.cc
index 02645308..0018d308 100644
--- a/src/tools/windows/dump_syms/dump_syms.cc
+++ b/src/tools/windows/dump_syms/dump_syms.cc
@@ -39,20 +39,14 @@
 using std::wstring;
 using google_airbag::PDBSourceLineWriter;
 
-int main(int argc, char **argv) {
+int wmain(int argc, wchar_t **argv) {
   if (argc < 2) {
-    fprintf(stderr, "Usage: %s <pdb file>\n", argv[0]);
-    return 1;
-  }
-
-  wchar_t filename[_MAX_PATH];
-  if (mbstowcs_s(NULL, filename, argv[1], _MAX_PATH) == -1) {
-    fprintf(stderr, "invalid multibyte character in %s\n", argv[1]);
+    fprintf(stderr, "Usage: %ws <pdb file>\n", argv[0]);
     return 1;
   }
 
   PDBSourceLineWriter writer;
-  if (!writer.Open(wstring(filename), PDBSourceLineWriter::ANY_FILE)) {
+  if (!writer.Open(wstring(argv[1]), PDBSourceLineWriter::ANY_FILE)) {
     fprintf(stderr, "Open failed\n");
     return 1;
   }
diff --git a/src/tools/windows/dump_syms/dump_syms.vcproj b/src/tools/windows/dump_syms/dump_syms.vcproj
index c0a3390f..360b7d9b 100644
--- a/src/tools/windows/dump_syms/dump_syms.vcproj
+++ b/src/tools/windows/dump_syms/dump_syms.vcproj
@@ -183,6 +183,10 @@
 				RelativePath="..\..\..\common\windows\pdb_source_line_writer.h"
 				>
 			</File>
+			<File
+				RelativePath="..\..\..\common\windows\string_utils-inl.h"
+				>
+			</File>
 		</Filter>
 		<Filter
 			Name="Resource Files"
diff --git a/src/tools/windows/symupload/symupload.cc b/src/tools/windows/symupload/symupload.cc
index 33f8e51f..602a0568 100644
--- a/src/tools/windows/symupload/symupload.cc
+++ b/src/tools/windows/symupload/symupload.cc
@@ -46,6 +46,8 @@
 #include <string>
 #include <vector>
 
+#include "common/windows/string_utils-inl.h"
+
 #include "common/windows/http_upload.h"
 #include "common/windows/pdb_source_line_writer.h"
 
@@ -55,6 +57,7 @@ using std::vector;
 using std::map;
 using google_airbag::HTTPUpload;
 using google_airbag::PDBSourceLineWriter;
+using google_airbag::WindowsStringUtils;
 
 // Extracts the file version information for the given filename,
 // as a string, for example, "1.2.3.4".  Returns true on success.
@@ -82,12 +85,13 @@ static bool GetFileVersionString(const wchar_t *filename, wstring *version) {
   wchar_t ver_string[24];
   VS_FIXEDFILEINFO *file_info =
     reinterpret_cast<VS_FIXEDFILEINFO*>(file_info_buffer);
-  _snwprintf_s(ver_string, sizeof(ver_string) / sizeof(wchar_t), _TRUNCATE,
-               L"%d.%d.%d.%d",
-               file_info->dwFileVersionMS >> 16,
-               file_info->dwFileVersionMS & 0xffff,
-               file_info->dwFileVersionLS >> 16,
-               file_info->dwFileVersionLS & 0xffff);
+  WindowsStringUtils::safe_swprintf(
+      ver_string, sizeof(ver_string) / sizeof(ver_string[0]),
+      L"%d.%d.%d.%d",
+      file_info->dwFileVersionMS >> 16,
+      file_info->dwFileVersionMS & 0xffff,
+      file_info->dwFileVersionLS >> 16,
+      file_info->dwFileVersionLS & 0xffff);
   *version = ver_string;
   return true;
 }
@@ -116,7 +120,13 @@ static bool DumpSymbolsToTempFile(const wchar_t *file,
   }
 
   FILE *temp_file = NULL;
+#if _MSC_VER >= 1400  // MSVC 2005/8
   if (_wfopen_s(&temp_file, temp_filename, L"w") != 0) {
+#else  // _MSC_VER >= 1400
+  // _wfopen_s was introduced in MSVC8.  Use _wfopen for earlier environments.
+  // Don't use it with MSVC8 and later, because it's deprecated.
+  if (!(temp_file = _wfopen(temp_filename, L"w"))) {
+#endif  // _MSC_VER >= 1400
     return false;
   }
 
@@ -148,8 +158,10 @@ int wmain(int argc, wchar_t *argv[]) {
   }
 
   wchar_t module_age_string[11];
-  _snwprintf_s(module_age_string, sizeof(module_age_string) / sizeof(wchar_t),
-               _TRUNCATE, L"0x%x", module_age);
+  WindowsStringUtils::safe_swprintf(
+      module_age_string,
+      sizeof(module_age_string) / sizeof(module_age_string[0]),
+      L"0x%x", module_age);
 
   map<wstring, wstring> parameters;
   parameters[L"module"] = module_basename;
diff --git a/src/tools/windows/symupload/symupload.vcproj b/src/tools/windows/symupload/symupload.vcproj
index ddd63fc1..c1458854 100755
--- a/src/tools/windows/symupload/symupload.vcproj
+++ b/src/tools/windows/symupload/symupload.vcproj
@@ -188,6 +188,10 @@
 				RelativePath="..\..\..\common\windows\pdb_source_line_writer.h"
 				>
 			</File>
+			<File
+				RelativePath="..\..\..\common\windows\string_utils-inl.h"
+				>
+			</File>
 		</Filter>
 		<Filter
 			Name="Resource Files"
-- 
cgit v1.2.1