Mercurial > trustbridge
comparison ui/main.cpp @ 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 | edf269b6e499 |
children | 80d1a80b3e8d |
comparison
equal
deleted
inserted
replaced
632:9a18f096129d | 633:6c090638b2b4 |
---|---|
39 | 39 |
40 int main(int argc, char **argv) | 40 int main(int argc, char **argv) |
41 { | 41 { |
42 /* First verify integrity even before calling QApplication*/ | 42 /* First verify integrity even before calling QApplication*/ |
43 #ifdef Q_OS_WIN | 43 #ifdef Q_OS_WIN |
44 DWORD sizeNeeded = GetModuleFileNameW (NULL, NULL, 0); | 44 { |
45 wchar_t wPath[sizeNeeded + 1]; | 45 wchar_t wPath[MAX_PATH]; |
46 char *utf8path = NULL; | 46 char *utf8path = NULL; |
47 | 47 |
48 if (sizeNeeded == 0) { | 48 if (!GetModuleFileNameW (NULL, wPath, MAX_PATH - 1)) { |
49 PRINTLASTERROR ("Failed to obtain module file name"); | 49 PRINTLASTERROR ("Failed to obtain module file name. Path too long?"); |
50 syslog_error_printf ("Integrity check failed."); | 50 syslog_error_printf ("Integrity check failed."); |
51 return -1; | 51 return -1; |
52 } | 52 } |
53 | 53 |
54 DWORD realSize = GetModuleFileNameW (NULL, wPath, sizeNeeded + 1); | 54 /* wPath might not be 0 terminated */ |
55 wPath[MAX_PATH - 1] = '\0'; | |
55 | 56 |
56 if (realSize != sizeNeeded) { | 57 utf8path = wchar_to_utf8 (wPath, wcsnlen(wPath, MAX_PATH)); |
57 ERRORPRINTF ("Module name changed"); | |
58 syslog_error_printf ("Integrity check failed."); | |
59 return -1; | |
60 } | |
61 | 58 |
62 utf8path = wchar_to_utf8 (wPath, sizeNeeded + 1); | 59 if (utf8path == NULL) { |
60 ERRORPRINTF ("Failed to convert module path to utf-8"); | |
61 syslog_error_printf ("Integrity check failed."); | |
62 return -1; | |
63 } | |
63 | 64 |
64 if (utf8path == NULL) { | 65 if (!verify_binary (utf8path, strlen(utf8path)) != VerifyValid) |
65 ERRORPRINTF ("Failed to convert module path to utf-8"); | 66 { |
66 syslog_error_printf ("Integrity check failed."); | 67 ERRORPRINTF ("Verification of the binary failed"); |
67 return -1; | 68 syslog_error_printf ("Integrity check failed."); |
68 } | 69 xfree(utf8path); |
70 #ifdef RELEASE_BUILD | |
71 return -1; | |
72 #endif | |
73 } | |
69 | 74 |
70 if (!verify_binary (utf8path, strlen(utf8path)) != VerifyValid) | |
71 { | |
72 syslog_error_printf ("Integrity check failed."); | |
73 xfree(utf8path); | 75 xfree(utf8path); |
74 #ifdef RELEASE_BUILD | |
75 return -1; | |
76 #endif | |
77 } | 76 } |
78 | |
79 xfree(utf8path); | |
80 #else | 77 #else |
81 if (!verify_binary ("/proc/self/exe", 14) != VerifyValid) | 78 if (!verify_binary ("/proc/self/exe", 14) != VerifyValid) |
82 { | 79 { |
83 syslog_error_printf ("Integrity check failed."); | 80 syslog_error_printf ("Integrity check failed."); |
84 #ifdef RELEASE_BUILD | 81 #ifdef RELEASE_BUILD |