changeset 633:6c090638b2b4

Use static buffer for module file name. According to the msdn examle the return value of getmodulefilename should be used to indicate success and not the size. And according to comments on that function on Windows 8.1 it does not return the needed size. So better be more robust and just use max_path as a limit.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 23 Jun 2014 15:29:48 +0200
parents 9a18f096129d
children 80d1a80b3e8d
files ui/main.cpp
diffstat 1 files changed, 28 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/ui/main.cpp	Mon Jun 23 14:56:11 2014 +0200
+++ b/ui/main.cpp	Mon Jun 23 15:29:48 2014 +0200
@@ -41,42 +41,39 @@
 {
     /* First verify integrity even before calling QApplication*/
 #ifdef Q_OS_WIN
-  DWORD sizeNeeded = GetModuleFileNameW (NULL, NULL, 0);
-  wchar_t wPath[sizeNeeded + 1];
-  char *utf8path = NULL;
-
-  if (sizeNeeded == 0) {
-      PRINTLASTERROR ("Failed to obtain module file name");
-      syslog_error_printf ("Integrity check failed.");
-      return -1;
-  }
-
-  DWORD realSize = GetModuleFileNameW (NULL, wPath, sizeNeeded + 1);
-
-  if (realSize != sizeNeeded) {
-      ERRORPRINTF ("Module name changed");
-      syslog_error_printf ("Integrity check failed.");
-      return -1;
-  }
+    {
+      wchar_t wPath[MAX_PATH];
+      char *utf8path = NULL;
 
-  utf8path = wchar_to_utf8 (wPath, sizeNeeded + 1);
-
-  if (utf8path == NULL) {
-      ERRORPRINTF ("Failed to convert module path to utf-8");
-      syslog_error_printf ("Integrity check failed.");
-      return -1;
-  }
+      if (!GetModuleFileNameW (NULL, wPath, MAX_PATH - 1)) {
+          PRINTLASTERROR ("Failed to obtain module file name. Path too long?");
+          syslog_error_printf ("Integrity check failed.");
+          return -1;
+      }
 
-  if (!verify_binary (utf8path, strlen(utf8path)) != VerifyValid)
-    {
-      syslog_error_printf ("Integrity check failed.");
-      xfree(utf8path);
+      /* wPath might not be 0 terminated */
+      wPath[MAX_PATH - 1] = '\0';
+
+      utf8path = wchar_to_utf8 (wPath, wcsnlen(wPath, MAX_PATH));
+
+      if (utf8path == NULL) {
+          ERRORPRINTF ("Failed to convert module path to utf-8");
+          syslog_error_printf ("Integrity check failed.");
+          return -1;
+      }
+
+      if (!verify_binary (utf8path, strlen(utf8path)) != VerifyValid)
+        {
+          ERRORPRINTF ("Verification of the binary failed");
+          syslog_error_printf ("Integrity check failed.");
+          xfree(utf8path);
 #ifdef RELEASE_BUILD
-      return -1;
+          return -1;
 #endif
+        }
+
+      xfree(utf8path);
     }
-
-  xfree(utf8path);
 #else
     if (!verify_binary ("/proc/self/exe", 14) != VerifyValid)
       {

http://wald.intevation.org/projects/trustbridge/