From 8a03dc1cc3a34ff70fb0fd9d4a0b617456980fb8 Mon Sep 17 00:00:00 2001 From: s1lentq Date: Sat, 14 Jan 2017 18:55:17 +0700 Subject: [PATCH] Fix crash: Use AMXX_Error instead LogError. --- reapi/src/amxxmodule.cpp | 22 ++++++++++++++++++++-- reapi/src/amxxmodule.h | 1 + reapi/src/hook_callback.h | 2 +- reapi/src/hook_manager.cpp | 9 +++++++-- reapi/src/hook_manager.h | 11 +++++++++-- reapi/src/natives/natives_hookchains.cpp | 2 +- 6 files changed, 39 insertions(+), 8 deletions(-) diff --git a/reapi/src/amxxmodule.cpp b/reapi/src/amxxmodule.cpp index 47a5402..61611d6 100644 --- a/reapi/src/amxxmodule.cpp +++ b/reapi/src/amxxmodule.cpp @@ -45,9 +45,9 @@ static struct funcreq_t //DECLARE_REQ(GetAmxVectorNull), //DECLARE_REQ(PrintSrvConsole), //DECLARE_REQ(GetModname), - //DECLARE_REQ(GetAmxScriptName), + DECLARE_REQ(GetAmxScriptName), //DECLARE_REQ(GetAmxScript), - //DECLARE_REQ(FindAmxScriptByAmx), + DECLARE_REQ(FindAmxScriptByAmx), //DECLARE_REQ(FindAmxScriptByName), DECLARE_REQ(SetAmxString), //DECLARE_REQ(SetAmxStringUTF8Char), @@ -219,6 +219,24 @@ NOINLINE void MF_LogError(AMX *amx, int err, const char *fmt, ...) g_amxxapi.LogError(amx, err, "[%s] %s", g_ModuleInfo.logtag, msg); } +NOINLINE void AMXX_Error(AMX *amx, const char *fmt, ...) +{ + char msg[2048]; + va_list arglst; + va_start(arglst, fmt); + vsnprintf(msg, sizeof msg, fmt, arglst); + va_end(arglst); + + auto scriptName = g_amxxapi.GetAmxScriptName(g_amxxapi.FindAmxScriptByAmx(amx)); + if (scriptName) + { + if ((scriptName = strrchr(scriptName, '\\'))) + scriptName++; + } + + g_amxxapi.Log("[%s] %s", scriptName, msg); +} + char* getAmxStringTemp(cell* src, char* dest, size_t max, size_t* len) { char* start = dest; diff --git a/reapi/src/amxxmodule.h b/reapi/src/amxxmodule.h index f6a3370..07b2de9 100644 --- a/reapi/src/amxxmodule.h +++ b/reapi/src/amxxmodule.h @@ -493,6 +493,7 @@ extern amxxapi_t g_amxxapi; void MF_Log(const char *fmt, ...); void MF_LogError(AMX *amx, int err, const char *fmt, ...); +void AMXX_Error(AMX *amx, const char *fmt, ...); char* getAmxStringTemp(cell* src, char* dest, size_t max, size_t* len = nullptr); void setAmxString(cell* dest, const char* string, size_t max); diff --git a/reapi/src/hook_callback.h b/reapi/src/hook_callback.h index 82d5d92..5984f34 100644 --- a/reapi/src/hook_callback.h +++ b/reapi/src/hook_callback.h @@ -217,7 +217,7 @@ NOINLINE R DLLEXPORT _callForward(const hook_t* hook, original_t original, volat } if (unlikely(!hookCtx->retVal.set)) { - g_amxxapi.LogError(fwd->GetAmx(), AMX_ERR_CALLBACK, "can't suppress original function call without new return value set"); + AMXX_Error(fwd->GetAmx(), "%s : Can't suppress original function call without new return value set", fwd->GetCallbackName()); continue; } diff --git a/reapi/src/hook_manager.cpp b/reapi/src/hook_manager.cpp index 922d11c..2886bfc 100644 --- a/reapi/src/hook_manager.cpp +++ b/reapi/src/hook_manager.cpp @@ -2,7 +2,7 @@ CHookManager g_hookManager; -int CHookManager::addHandler(AMX* amx, int func, int forward, bool post) const +int CHookManager::addHandler(AMX* amx, int func, const char *funcname, int forward, bool post) const { auto hook = m_hooklist.getHookSafe(func); @@ -13,7 +13,7 @@ int CHookManager::addHandler(AMX* amx, int func, int forward, bool post) const } auto& dest = post ? hook->post : hook->pre; - dest.push_back(new CAmxxHook(amx, forward)); + dest.push_back(new CAmxxHook(amx, funcname, forward)); int id = func * MAX_HOOK_FORWARDS + dest.size(); return post ? -id : id; // use unsigned ids for post hooks } @@ -23,6 +23,11 @@ AMX* CAmxxHook::GetAmx() const return m_amx; } +const char *CAmxxHook::GetCallbackName() const +{ + return m_CallbackName; +} + int CAmxxHook::GetIndex() const { return m_index; diff --git a/reapi/src/hook_manager.h b/reapi/src/hook_manager.h index 40e023d..daae87e 100644 --- a/reapi/src/hook_manager.h +++ b/reapi/src/hook_manager.h @@ -12,15 +12,22 @@ enum fwdstate class CAmxxHook { public: - CAmxxHook(AMX* amx, int index) : m_index(index), m_state(FSTATE_ENABLED), m_amx(amx) {}; + CAmxxHook(AMX* amx, const char *funcname, int index) : m_index(index), m_state(FSTATE_ENABLED), m_amx(amx) + { + strncpy(m_CallbackName, funcname, sizeof(m_CallbackName) - 1); + m_CallbackName[sizeof(m_CallbackName) - 1] = '\0'; + }; int GetIndex() const; fwdstate GetState() const; AMX* GetAmx() const; + const char *GetCallbackName() const; + void SetState(fwdstate st); private: int m_index; + char m_CallbackName[64]; fwdstate m_state; AMX* m_amx; }; @@ -29,7 +36,7 @@ class CHookManager { public: void clearHandlers() const; - cell addHandler(AMX* amx, int func, int forward, bool post) const; + cell addHandler(AMX* amx, int func, const char *funcname, int forward, bool post) const; hook_t* getHook(size_t func) const; CAmxxHook* getAmxxHook(cell hook) const; diff --git a/reapi/src/natives/natives_hookchains.cpp b/reapi/src/natives/natives_hookchains.cpp index ca545fc..16e51a0 100644 --- a/reapi/src/natives/natives_hookchains.cpp +++ b/reapi/src/natives/natives_hookchains.cpp @@ -46,7 +46,7 @@ cell AMX_NATIVE_CALL RegisterHookChain(AMX *amx, cell *params) return INVALID_HOOKCHAIN; } - return g_hookManager.addHandler(amx, func, fwid, post != 0); + return g_hookManager.addHandler(amx, func, funcname, fwid, post != 0); } /*