@@ -35,88 +35,56 @@ namespace internal {
3535// Returns an empty string on failure.
3636static std::wstring GetExecutablePath () {
3737 std::vector<wchar_t > buffer;
38- const size_t kInitialBufferSize = MAX_PATH + 1 ;
39-
4038 // First attempt with MAX_PATH + 1
41- // Note: std::vector::resize can throw std::bad_alloc if allocation fails.
42- // Per requirements, no try/catch here.
43- buffer.reserve (kInitialBufferSize );
44- buffer.resize (kInitialBufferSize );
39+ buffer.reserve (MAX_PATH+1 );
40+ buffer.resize (MAX_PATH+1 );
4541
4642 DWORD length = GetModuleFileNameW (NULL , buffer.data (),
4743 static_cast <DWORD>(buffer.size ()));
4844
4945 if (length == 0 ) {
5046 DWORD error_code = GetLastError ();
51- LogError (LOG_TAG " GetModuleFileNameW (initial) failed. Error: %u" ,
52- error_code);
47+ LogError (LOG_TAG " Failed to get executable path. Error: %u" , error_code);
5348 return std::wstring ();
5449 }
5550
5651 if (length < buffer.size ()) {
52+ // Executable path fit in our buffer, return it.
5753 return std::wstring (buffer.data (), length);
58- }
59-
60- // If length == buffer.size(), check if it was due to insufficient buffer.
61- if (length == buffer.size ()) { // Could also be length >= buffer.size()
54+ } else {
6255 DWORD error_code = GetLastError ();
6356 if (error_code == ERROR_INSUFFICIENT_BUFFER) {
6457 // Buffer was too small, try a larger one.
65- const size_t kMaxExecutablePathSize = 65536 ;
66-
67- // If initial buffer was already this big or bigger, no point retrying.
68- if (kInitialBufferSize >= kMaxExecutablePathSize ) {
69- LogError (
70- LOG_TAG
71- " Initial buffer attempt failed due to insufficient size, but "
72- " initial size (%zu) >= kMaxExecutablePathSize (%zu)." ,
73- kInitialBufferSize , kMaxExecutablePathSize );
74- return std::wstring ();
75- }
58+ const size_t kLongPathMax = 65536 +1 ;
7659
77- buffer.clear (); // Optional: clear contents before large resize
78- // Note: std::vector::resize can throw std::bad_alloc.
79- buffer.reserve (kMaxExecutablePathSize );
80- buffer.resize (kMaxExecutablePathSize );
60+ buffer.clear ();
61+ buffer.reserve (kLongPathMax );
62+ buffer.resize (kLongPathMax );
8163
8264 DWORD length_large = GetModuleFileNameW (
8365 NULL , buffer.data (), static_cast <DWORD>(buffer.size ()));
8466
8567 if (length_large == 0 ) {
8668 DWORD error_code_large = GetLastError ();
87- LogError (LOG_TAG " GetModuleFileNameW (large buffer) failed. Error: %u" ,
88- error_code_large);
69+ LogError (LOG_TAG " Failed to get executable long path. Error: %u" , error_code_large);
8970 return std::wstring ();
9071 }
9172
9273 if (length_large < buffer.size ()) {
9374 return std::wstring (buffer.data (), length_large);
94- }
95-
96- // If length_large is still equal to or greater than buffer size,
97- // the path is too long even for the large buffer, or truncated.
98- LogError (
99- LOG_TAG
100- " GetModuleFileNameW (large buffer) result too long or truncated. "
101- " Length: %u, Buffer Size: %zu" ,
102- length_large, buffer.size ());
75+ } else {
76+ // If length_large is still equal to or greater than buffer size, regardless of error,
77+ // the path is too long for even the large buffer.
78+ DWORD error_code_large = GetLastError ();
79+ LogError (LOG_TAG " Executable path too long. Error: %u" , error_code_large);
10380 return std::wstring ();
10481
10582 } else {
106- // length == buffer.size() but not ERROR_INSUFFICIENT_BUFFER.
107- LogError (
108- LOG_TAG
109- " GetModuleFileNameW (initial) returned full buffer but error is not "
110- " ERROR_INSUFFICIENT_BUFFER. Error: %u" ,
111- error_code);
83+ // length >= buffer.size() but not ERROR_INSUFFICIENT_BUFFER.
84+ LogError (LOG_TAG " Failed to get executable path. Error: %u" , error_code);
11285 return std::wstring ();
11386 }
11487 }
115-
116- // Should not be reached if logic is correct, but as a fallback.
117- // This case implies length > buffer.size() initially, which is unexpected.
118- LogError (LOG_TAG " GetModuleFileNameW (initial) unexpected state. Length: %u" , length);
119- return std::wstring ();
12088}
12189
12290// Helper function to calculate SHA256 hash of a file.
0 commit comments