@@ -35,88 +35,56 @@ namespace internal {
35
35
// Returns an empty string on failure.
36
36
static std::wstring GetExecutablePath () {
37
37
std::vector<wchar_t > buffer;
38
- const size_t kInitialBufferSize = MAX_PATH + 1 ;
39
-
40
38
// 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 );
45
41
46
42
DWORD length = GetModuleFileNameW (NULL , buffer.data (),
47
43
static_cast <DWORD>(buffer.size ()));
48
44
49
45
if (length == 0 ) {
50
46
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);
53
48
return std::wstring ();
54
49
}
55
50
56
51
if (length < buffer.size ()) {
52
+ // Executable path fit in our buffer, return it.
57
53
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 {
62
55
DWORD error_code = GetLastError ();
63
56
if (error_code == ERROR_INSUFFICIENT_BUFFER) {
64
57
// 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 ;
76
59
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 );
81
63
82
64
DWORD length_large = GetModuleFileNameW (
83
65
NULL , buffer.data (), static_cast <DWORD>(buffer.size ()));
84
66
85
67
if (length_large == 0 ) {
86
68
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);
89
70
return std::wstring ();
90
71
}
91
72
92
73
if (length_large < buffer.size ()) {
93
74
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);
103
80
return std::wstring ();
104
81
105
82
} 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);
112
85
return std::wstring ();
113
86
}
114
87
}
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 ();
120
88
}
121
89
122
90
// Helper function to calculate SHA256 hash of a file.
0 commit comments