Skip to content

Commit

Permalink
[win32] Also fail if GetModuleFileName() returns a value >= _countof(…
Browse files Browse the repository at this point in the history
…dll_filename).

On Windows XP, this indicates that the buffer size was too small.

svrplus.c and rp-config.c both checked for this, but they did not check
for GetLastError() != ERROR_SUCCESS, so they check for that now.
  • Loading branch information
GerbilSoft committed Jul 24, 2022
1 parent e845f51 commit 69e4d89
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/libi18n/i18n.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ static void rp_i18n_init_int(void)
// We'll assume it's ASCII and do a simple conversion to Unicode.
SetLastError(ERROR_SUCCESS); // required for XP
dwResult = GetModuleFileName(HINST_THISCOMPONENT,
tpathname, ARRAY_SIZE(tpathname));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
tpathname, _countof(tpathname));
if (dwResult == 0 || dwResult >= _countof(tpathname) || GetLastError() != ERROR_SUCCESS) {
// Cannot get the current module filename.
// TODO: Windows XP doesn't SetLastError() if the
// filename is too big for the buffer.
Expand Down
2 changes: 1 addition & 1 deletion src/libromdata/data/AmiiboData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ string AmiiboDataPrivate::getAmiiboBinFilename(AmiiboBinFileType amiiboBinFileTy
SetLastError(ERROR_SUCCESS); // required for XP
DWORD dwResult = GetModuleFileName(HINST_THISCOMPONENT,
dll_filename, _countof(dll_filename));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
if (dwResult == 0 || dwResult >= _countof(dll_filename) || GetLastError() != ERROR_SUCCESS) {
// Cannot get the DLL filename.
// TODO: Windows XP doesn't SetLastError() if the
// filename is too big for the buffer.
Expand Down
2 changes: 1 addition & 1 deletion src/libromdata/img/ExecRpDownload_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int CacheManager::execRpDownload(const string &filteredCacheKey)
SetLastError(ERROR_SUCCESS); // required for XP
DWORD dwResult = GetModuleFileName(HINST_THISCOMPONENT,
dll_filename, _countof(dll_filename));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
if (dwResult == 0 || dwResult >= _countof(dll_filename) || GetLastError() != ERROR_SUCCESS) {
// Cannot get the DLL filename.
// TODO: Windows XP doesn't SetLastError() if the
// filename is too big for the buffer.
Expand Down
2 changes: 1 addition & 1 deletion src/libwin32common/DelayLoadHelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static HMODULE WINAPI rp_loadLibrary(LPCSTR pszModuleName)
SetLastError(ERROR_SUCCESS); // required for XP
dwResult = GetModuleFileName(HINST_THISCOMPONENT,
dll_fullpath, _countof(dll_fullpath));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
if (dwResult == 0 || dwResult >= _countof(dll_fullpath) || GetLastError() != ERROR_SUCCESS) {
// Cannot get the current module filename.
// TODO: Windows XP doesn't SetLastError() if the
// filename is too big for the buffer.
Expand Down
2 changes: 1 addition & 1 deletion src/libwin32common/RegKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ LONG RegKey::RegisterComObject(HINSTANCE hInstance, REFCLSID rclsid,
TCHAR dll_filename[MAX_PATH];
SetLastError(ERROR_SUCCESS); // required for XP
DWORD dwResult = GetModuleFileName(hInstance, dll_filename, _countof(dll_filename));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
if (dwResult == 0 || dwResult >= _countof(dll_filename) || GetLastError() != ERROR_SUCCESS) {
// Cannot get the DLL filename.
// TODO: Windows XP doesn't SetLastError() if the
// filename is too big for the buffer.
Expand Down
2 changes: 1 addition & 1 deletion src/svrplus/svrplus.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ static InstallServerResult InstallServer(bool isUninstall, bool is64, DWORD *pEr
szModuleFn = GetModuleFileName(HINST_THISCOMPONENT, &args[14], MAX_PATH);
assert(szModuleFn != 0);
assert(szModuleFn < MAX_PATH);
if (szModuleFn == 0 || szModuleFn >= MAX_PATH) {
if (szModuleFn == 0 || szModuleFn >= MAX_PATH || GetLastError() != ERROR_SUCCESS) {
// TODO: add an error message for the MAX_PATH case?
return ISR_FATAL_ERROR;
}
Expand Down
2 changes: 1 addition & 1 deletion src/win32/DllMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
SetLastError(ERROR_SUCCESS); // required for XP
DWORD dwResult = GetModuleFileName(hInstance,
dll_filename, _countof(dll_filename));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
if (dwResult == 0 || dwResult >= _countof(dll_filename) || GetLastError() != ERROR_SUCCESS) {
// Cannot get the DLL filename.
// TODO: Windows XP doesn't SetLastError() if the
// filename is too big for the buffer.
Expand Down
2 changes: 1 addition & 1 deletion src/win32/config/rp-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
FAIL_MESSAGE(_T("Failed to allocate memory for the EXE path."));
SetLastError(ERROR_SUCCESS); // required for XP
exe_path_len = GetModuleFileName(hInstance, exe_path, EXE_PATH_LEN);
if (exe_path_len == 0 || exe_path_len >= EXE_PATH_LEN)
if (exe_path_len == 0 || exe_path_len >= EXE_PATH_LEN || GetLastError() != ERROR_SUCCESS)
FAIL_MESSAGE(_T("Failed to get the EXE path."));

// Find the last backslash.
Expand Down

0 comments on commit 69e4d89

Please sign in to comment.