From 5d21350e9e6db89891304d28922b93ce076e8c91 Mon Sep 17 00:00:00 2001 From: Peace-Maker Date: Mon, 22 Jan 2018 00:11:28 +0100 Subject: [PATCH] Fix not clearing return address cache after use The esp pointer wasn't removed from the map after the function was called and the original return address was retrieved. If the same function was called again with the same esp this would fail due to there already being an (old) return address associated with the esp. --- DynamicHooks/hook.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/DynamicHooks/hook.cpp b/DynamicHooks/hook.cpp index 8d96d21..eee9c35 100644 --- a/DynamicHooks/hook.cpp +++ b/DynamicHooks/hook.cpp @@ -189,10 +189,19 @@ ReturnAction_t CHook::HookHandler(HookType_t eHookType) void* __cdecl CHook::GetReturnAddress(void* pESP) { ReturnAddressMap::Result r = m_RetAddr.find(pESP); + assert(r.found()); if (!r.found()) + { puts("ESP not present."); + return NULL; + } - return r->value; + void *pRetAddr = r->value; + + // Clear the stack address from the cache now that we ran the post hook code. + m_RetAddr.remove(r); + + return pRetAddr; } void __cdecl CHook::SetReturnAddress(void* pRetAddr, void* pESP)