Skip to content

Commit 0a3ab89

Browse files
authored
Fix + Tests: Add more tests covering the modules API, fix error handling bug that were found. (microsoft#177)
* Fix: Handle more error cases in module API. - `DetoursFindFunction` wasn't gracefully handling NULL function name. - `DetourEnumerateModules` wasn't resetting GLE on success. - `DetourEnumerateExports` wasn't gracefully handling NULL export callback. - `DetourEnumerateImports` wasn't gracefully handling NULL arguments. * Tests: Add more tests covering the modules API. Test basic functionality and error handling of the Detours Module API. * DetourLoadImageHlp * DetourFindFunction * DetourEnumerateModules * DetourEnumerateExports * DetourEnumerateImports * DetourGetSizeOfPayloads * DetourFindPayload * DetourFindPayloadEx * DetourRestoreAfterWithEx
1 parent 108ceef commit 0a3ab89

File tree

2 files changed

+549
-1
lines changed

2 files changed

+549
-1
lines changed

src/modules.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ PDETOUR_SYM_INFO DetourLoadImageHlp(VOID)
142142
PVOID WINAPI DetourFindFunction(_In_ LPCSTR pszModule,
143143
_In_ LPCSTR pszFunction)
144144
{
145+
if (pszFunction == NULL) {
146+
SetLastError(ERROR_INVALID_PARAMETER);
147+
return NULL;
148+
}
149+
145150
/////////////////////////////////////////////// First, try GetProcAddress.
146151
//
147152
#pragma prefast(suppress:28752, "We don't do the unicode conversion for LoadLibraryExA.")
@@ -277,6 +282,7 @@ HMODULE WINAPI DetourEnumerateModules(_In_opt_ HMODULE hModuleLast)
277282
continue;
278283
}
279284

285+
SetLastError(NO_ERROR);
280286
return (HMODULE)pDosHeader;
281287
}
282288
#pragma prefast(suppress:28940, "A bad pointer means this probably isn't a PE header.")
@@ -456,6 +462,11 @@ BOOL WINAPI DetourEnumerateExports(_In_ HMODULE hModule,
456462
_In_opt_ PVOID pContext,
457463
_In_ PF_DETOUR_ENUMERATE_EXPORT_CALLBACK pfExport)
458464
{
465+
if (pfExport == NULL) {
466+
SetLastError(ERROR_INVALID_PARAMETER);
467+
return FALSE;
468+
}
469+
459470
PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;
460471
if (hModule == NULL) {
461472
pDosHeader = (PIMAGE_DOS_HEADER)GetModuleHandleW(NULL);
@@ -658,6 +669,11 @@ BOOL WINAPI DetourEnumerateImports(_In_opt_ HMODULE hModule,
658669
_In_opt_ PF_DETOUR_IMPORT_FILE_CALLBACK pfImportFile,
659670
_In_opt_ PF_DETOUR_IMPORT_FUNC_CALLBACK pfImportFunc)
660671
{
672+
if (pfImportFile == NULL || pfImportFunc == NULL) {
673+
SetLastError(ERROR_INVALID_PARAMETER);
674+
return FALSE;
675+
}
676+
661677
_DETOUR_ENUMERATE_IMPORTS_THUNK_CONTEXT const context = { pContext, pfImportFunc };
662678

663679
return DetourEnumerateImportsEx(hModule,

0 commit comments

Comments
 (0)