[PATCH 1 of 2] Use static buffer for module file name

Wald Commits scm-commit at wald.intevation.org
Mon Jun 23 16:43:12 CEST 2014


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1403530188 -7200
# Node ID 6c090638b2b453d85b14c54d1a6c3eae95acc83c
# Parent  9a18f096129db70cbfb76b6c56de8f00f8ea4f5c
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.

diff -r 9a18f096129d -r 6c090638b2b4 ui/main.cpp
--- 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;
+    {
+      wchar_t wPath[MAX_PATH];
+      char *utf8path = NULL;
 
-  if (sizeNeeded == 0) {
-      PRINTLASTERROR ("Failed to obtain module file name");
-      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;
+      }
 
-  DWORD realSize = GetModuleFileNameW (NULL, wPath, sizeNeeded + 1);
+      /* wPath might not be 0 terminated */
+      wPath[MAX_PATH - 1] = '\0';
 
-  if (realSize != sizeNeeded) {
-      ERRORPRINTF ("Module name changed");
-      syslog_error_printf ("Integrity check failed.");
-      return -1;
-  }
+      utf8path = wchar_to_utf8 (wPath, wcsnlen(wPath, MAX_PATH));
 
-  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 (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;
+#endif
+        }
 
-  if (!verify_binary (utf8path, strlen(utf8path)) != VerifyValid)
-    {
-      syslog_error_printf ("Integrity check failed.");
       xfree(utf8path);
-#ifdef RELEASE_BUILD
-      return -1;
-#endif
     }
-
-  xfree(utf8path);
 #else
     if (!verify_binary ("/proc/self/exe", 14) != VerifyValid)
       {


More information about the Trustbridge-commits mailing list