Skip to content

Commit 961977a

Browse files
committed
make Memory-management-APIs can avoid Use-after-free, Double-free and Over-bound-access memory errors.
1 parent 1fe5b0e commit 961977a

File tree

2 files changed

+279
-72
lines changed

2 files changed

+279
-72
lines changed

src/detours.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@
1818

1919
#define NOTHROW
2020

21+
//////////////////////////////////////////////////////////////////////////////
22+
//
23+
24+
extern "C" void __cdecl _assert(const char* expr, const char* filename, unsigned lineno);
25+
void DetourAssert(const char* expr, const char* filename, unsigned lineno)
26+
{
27+
DWORD dwLastError = GetLastError();
28+
SetLastError(dwLastError);
29+
int last_error_mode = _set_error_mode(_OUT_TO_MSGBOX);
30+
_assert(expr, filename, lineno);
31+
if (last_error_mode != _OUT_TO_MSGBOX) {
32+
_set_error_mode(last_error_mode);
33+
}
34+
SetLastError(dwLastError);
35+
}
36+
2137
//////////////////////////////////////////////////////////////////////////////
2238
//
2339
struct _DETOUR_ALIGN
@@ -2225,7 +2241,7 @@ static BOOL GetCurrentProcessSnapshot(PBYTE* snapshot, PSYSTEM_PROCESS_INFORMATI
22252241

22262242
// this never returns NULL as the current process id is always present in the processes snapshot
22272243
*procInfo = FindProcess(*snapshot, GetCurrentProcessId());
2228-
assert(procInfo);
2244+
DETOUR_ASSERT(procInfo);
22292245

22302246
return TRUE;
22312247
}
@@ -2257,13 +2273,13 @@ BOOL WINAPI DetourUpdateAllOtherThreads()
22572273
HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION | THREAD_SET_CONTEXT, FALSE, threadId);
22582274
if (hThread) {
22592275
LONG error = DetourUpdateThreadEx(hThread, TRUE);
2260-
assert(error == NO_ERROR);
2276+
DETOUR_ASSERT(error == NO_ERROR);
22612277
if (error) {
22622278
DETOUR_TRACE(("DetourUpdateThreadEx failed, error=%d\n", error));
22632279
// Reset s_nPendingError so that subsequent threads can continue to try to call the DetourUpdateThreadEx function
2264-
assert(error == s_nPendingError);
2280+
DETOUR_ASSERT(error == s_nPendingError);
22652281
// When the console program is terminated, s_nPendingError == ERROR_ACCESS_DENIED may be caused when the SuspendThread is called in DetourUpdateThreadEx
2266-
assert(s_nPendingError == ERROR_ACCESS_DENIED);
2282+
DETOUR_ASSERT(s_nPendingError == ERROR_ACCESS_DENIED);
22672283
s_nPendingError = NO_ERROR;
22682284
}
22692285
}

0 commit comments

Comments
 (0)