Skip to content

Commit 9e09b5e

Browse files
committed
Updated formatting and errors.
1 parent 24398f9 commit 9e09b5e

File tree

1 file changed

+87
-65
lines changed

1 file changed

+87
-65
lines changed

analytics/src/analytics_windows.cc

Lines changed: 87 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
#include <wincrypt.h>
1818
#include <windows.h>
1919

20-
#include <cstdlib> // Required for _wpgmptr
21-
#include <cstring> // For memcmp
22-
#include <string> // Required for std::wstring
20+
#include <cstdlib>
21+
#include <cstring>
22+
#include <string>
2323
#include <vector>
24-
#include <string> // Required for std::wstring
25-
#include <cstdlib> // Required for _wpgmptr
26-
#include <cstring> // For memcmp
27-
#include "app/src/log.h" //NOLINT
24+
25+
#include "app/src/log.h"
2826

2927
namespace firebase {
3028
namespace analytics {
@@ -33,46 +31,50 @@ namespace internal {
3331
// Helper function to retrieve the full path of the current executable.
3432
// Returns an empty string on failure.
3533
static std::wstring GetExecutablePath() {
36-
std::wstring executable_path_str;
37-
wchar_t* wpgmptr_val = nullptr;
34+
std::wstring executable_path_str;
35+
wchar_t* wpgmptr_val = nullptr;
3836

39-
// Prefer _get_wpgmptr()
40-
errno_t err_w = _get_wpgmptr(&wpgmptr_val);
41-
if (err_w == 0 && wpgmptr_val != nullptr && wpgmptr_val[0] != L'\0') {
42-
executable_path_str = wpgmptr_val;
43-
} else {
44-
// Fallback to _get_pgmptr() and convert to wide string
45-
char* pgmptr_val = nullptr;
46-
errno_t err_c = _get_pgmptr(&pgmptr_val);
47-
if (err_c == 0 && pgmptr_val != nullptr && pgmptr_val[0] != '\0') {
48-
// Convert narrow string to wide string using CP_ACP (system default ANSI code page)
49-
int wide_char_count = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, pgmptr_val, -1, NULL, 0);
50-
if (wide_char_count == 0) { // Failure if count is 0
51-
DWORD conversion_error = GetLastError();
52-
LogError("GetExecutablePath: MultiByteToWideChar failed to calculate size for _get_pgmptr path. Error: %u", conversion_error);
53-
return L"";
54-
}
55-
56-
std::vector<wchar_t> wide_path_buffer(wide_char_count);
57-
if (MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, pgmptr_val, -1, wide_path_buffer.data(), wide_char_count) == 0) {
58-
DWORD conversion_error = GetLastError();
59-
LogError("GetExecutablePath: MultiByteToWideChar failed to convert _get_pgmptr path. Error: %u", conversion_error);
60-
return L"";
61-
}
62-
executable_path_str = wide_path_buffer.data();
63-
} else {
64-
// Both _get_wpgmptr and _get_pgmptr failed or returned empty/null
65-
LogError("GetExecutablePath: Failed to retrieve executable path using both _get_wpgmptr (err: %d) and _get_pgmptr (err: %d).", err_w, err_c);
66-
return L"";
67-
}
68-
}
37+
// Prefer _get_wpgmptr()
38+
errno_t err_w = _get_wpgmptr(&wpgmptr_val);
39+
if (err_w == 0 && wpgmptr_val != nullptr && wpgmptr_val[0] != L'\0') {
40+
executable_path_str = wpgmptr_val;
41+
} else {
42+
// Fallback to _get_pgmptr() and convert to wide string
43+
char* pgmptr_val = nullptr;
44+
errno_t err_c = _get_pgmptr(&pgmptr_val);
45+
if (err_c == 0 && pgmptr_val != nullptr && pgmptr_val[0] != '\0') {
46+
// Convert narrow string to wide string using CP_ACP (system default ANSI
47+
// code page)
48+
int wide_char_count = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS,
49+
pgmptr_val, -1, NULL, 0);
50+
if (wide_char_count == 0) { // Failure if count is 0
51+
DWORD conversion_error = GetLastError();
52+
LogError(
53+
"VerifyAndLoadAnalyticsLibrary: Invalid executable path. Error: %u",
54+
conversion_error);
55+
return L"";
56+
}
6957

70-
// Safeguard if path somehow resolved to empty despite initial checks.
71-
if (executable_path_str.empty()) {
72-
LogError("GetExecutablePath: Executable path resolved to an empty string.");
58+
std::vector<wchar_t> wide_path_buffer(wide_char_count);
59+
if (MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, pgmptr_val, -1,
60+
wide_path_buffer.data(), wide_char_count) == 0) {
61+
DWORD conversion_error = GetLastError();
62+
LogError(
63+
"VerifyAndLoadAnalyticsLibrary: Invalid executable path. Error: %u",
64+
conversion_error);
7365
return L"";
66+
}
67+
executable_path_str = wide_path_buffer.data();
68+
} else {
69+
// Both _get_wpgmptr and _get_pgmptr failed or returned empty/null
70+
LogError(
71+
"VerifyAndLoadAnalyticsLibrary: Can't determine executable "
72+
"directory. Errors: %d, %d",
73+
err_w, err_c);
74+
return L"";
7475
}
75-
return executable_path_str;
76+
}
77+
return executable_path_str;
7678
}
7779

7880
// Helper function to calculate SHA256 hash of a file.
@@ -83,7 +85,10 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
8385

8486
if (SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
8587
DWORD dwError = GetLastError();
86-
LogError("CalculateFileSha256: SetFilePointer failed. Error: %u", dwError);
88+
LogError(
89+
"VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.SetFilePointer "
90+
"failed. Error: %u",
91+
dwError);
8792
return result_hash_value;
8893
}
8994

@@ -93,14 +98,19 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
9398
if (!CryptAcquireContextW(&hProv, NULL, NULL, PROV_RSA_AES,
9499
CRYPT_VERIFYCONTEXT)) {
95100
DWORD dwError = GetLastError();
96-
LogError("CalculateFileSha256: CryptAcquireContextW failed. Error: %u",
97-
dwError);
101+
LogError(
102+
"VerifyAndLoadAnalyticsLibrary: "
103+
"CalculateFileSha256.CryptAcquireContextW failed. Error: %u",
104+
dwError);
98105
return result_hash_value;
99106
}
100107

101108
if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash)) {
102109
DWORD dwError = GetLastError();
103-
LogError("CalculateFileSha256: CryptCreateHash failed. Error: %u", dwError);
110+
LogError(
111+
"VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.CryptCreateHash "
112+
"failed. Error: %u",
113+
dwError);
104114
CryptReleaseContext(hProv, 0);
105115
return result_hash_value;
106116
}
@@ -113,7 +123,10 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
113123
bReadSuccessLoop = ReadFile(hFile, rgbFile, sizeof(rgbFile), &cbRead, NULL);
114124
if (!bReadSuccessLoop) {
115125
DWORD dwError = GetLastError();
116-
LogError("CalculateFileSha256: ReadFile failed. Error: %u", dwError);
126+
LogError(
127+
"VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.ReadFile failed. "
128+
"Error: %u",
129+
dwError);
117130
CryptDestroyHash(hHash);
118131
CryptReleaseContext(hProv, 0);
119132
return result_hash_value;
@@ -123,7 +136,10 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
123136
}
124137
if (!CryptHashData(hHash, rgbFile, cbRead, 0)) {
125138
DWORD dwError = GetLastError();
126-
LogError("CalculateFileSha256: CryptHashData failed. Error: %u", dwError);
139+
LogError(
140+
"VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.CryptHashData "
141+
"failed. Error: %u",
142+
dwError);
127143
CryptDestroyHash(hHash);
128144
CryptReleaseContext(hProv, 0);
129145
return result_hash_value;
@@ -136,7 +152,8 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
136152
0)) {
137153
DWORD dwError = GetLastError();
138154
LogError(
139-
"CalculateFileSha256: CryptGetHashParam (HP_HASHSIZE) failed. Error: "
155+
"VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.CryptGetHashParam "
156+
"(HP_HASHSIZE) failed. Error: "
140157
"%u",
141158
dwError);
142159
CryptDestroyHash(hHash);
@@ -149,7 +166,8 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
149166
&cbHashValue, 0)) {
150167
DWORD dwError = GetLastError();
151168
LogError(
152-
"CalculateFileSha256: CryptGetHashParam (HP_HASHVAL) failed. Error: %u",
169+
"VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.CryptGetHashParam "
170+
"(HP_HASHVAL) failed. Error: %u",
153171
dwError);
154172
result_hash_value.clear();
155173
CryptDestroyHash(hHash);
@@ -185,23 +203,27 @@ HMODULE VerifyAndLoadAnalyticsLibrary(
185203
return nullptr;
186204
}
187205

188-
std::wstring executable_path_str = GetExecutablePath();
206+
std::wstring executable_path_str = GetExecutablePath();
189207

190-
if (executable_path_str.empty()) {
191-
// GetExecutablePath() is expected to log specific errors.
192-
// This log indicates the failure to proceed within this function.
193-
LogError("VerifyAndLoadAnalyticsLibrary: Failed to determine executable path via GetExecutablePath(), cannot proceed.");
194-
return nullptr;
195-
}
208+
if (executable_path_str.empty()) {
209+
// GetExecutablePath() is expected to log specific errors.
210+
// This log indicates the failure to proceed within this function.
211+
LogError(
212+
"VerifyAndLoadAnalyticsLibrary: Failed to determine executable path "
213+
"via GetExecutablePath(), cannot proceed.");
214+
return nullptr;
215+
}
196216

197-
size_t last_slash_pos = executable_path_str.find_last_of(L"\\");
198-
if (last_slash_pos == std::wstring::npos) {
199-
// Log message updated to avoid using %ls for executable_path_str
200-
LogError("VerifyAndLoadAnalyticsLibrary: Could not determine executable directory from retrieved path (no backslash found).");
201-
return nullptr;
202-
}
217+
size_t last_slash_pos = executable_path_str.find_last_of(L"\\");
218+
if (last_slash_pos == std::wstring::npos) {
219+
// Log message updated to avoid using %ls for executable_path_str
220+
LogError(
221+
"VerifyAndLoadAnalyticsLibrary: Could not determine executable "
222+
"directory from retrieved path (no backslash found).");
223+
return nullptr;
224+
}
203225

204-
std::wstring full_dll_path_str =
226+
std::wstring full_dll_path_str =
205227
executable_path_str.substr(0, last_slash_pos + 1);
206228
full_dll_path_str += library_filename; // library_filename is the filename
207229

0 commit comments

Comments
 (0)