Skip to content

Commit

Permalink
Add warning if a plugin tries to register an AMX native whose name is…
Browse files Browse the repository at this point in the history
… registered
  • Loading branch information
Hual authored and Y_Less committed Dec 14, 2022
1 parent 029ed45 commit c634f80
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Server/Components/Pawn/Plugin/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static const void
reinterpret_cast<void*>(&amx_PushArray),
reinterpret_cast<void*>(&amx_PushString),
reinterpret_cast<void*>(&amx_RaiseError),
reinterpret_cast<void*>(&amx_Register),
reinterpret_cast<void*>(&amx_RegisterChecked),
reinterpret_cast<void*>(&amx_Release),
reinterpret_cast<void*>(&amx_SetCallback),
reinterpret_cast<void*>(&amx_SetDebugHook),
Expand Down
49 changes: 49 additions & 0 deletions Server/Components/Pawn/Script/Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdarg.h>

#include "Script.hpp"
#include "../Manager/Manager.hpp"

extern "C"
{
Expand Down Expand Up @@ -307,3 +308,51 @@ int AMXAPI amx_StrSize(const cell* cstr, int* length)
} /* if */
return AMX_ERR_NONE;
}

static AMX_NATIVE findfunction(const char* name, const AMX_NATIVE_INFO* list, int number)
{
int i;

assert(list != NULL);
for (i = 0; list[i].name != NULL && (i < number || number == -1); i++)
if (strcmp(name, list[i].name) == 0)
return list[i].func;
return NULL;
}

int AMXAPI amx_RegisterChecked(AMX* amx, const AMX_NATIVE_INFO* list, int number)
{
AMX_FUNCPART* func;
AMX_HEADER* hdr;
int i, numnatives, err;
AMX_NATIVE funcptr;

hdr = (AMX_HEADER*)amx->base;
assert(hdr != NULL);
assert(hdr->magic == AMX_MAGIC);
assert(hdr->natives <= hdr->libraries);
numnatives = NUMENTRIES(hdr, natives, libraries);

err = AMX_ERR_NONE;
func = GETENTRY(hdr, natives, 0);
for (i = 0; i < numnatives; i++)
{
funcptr = (list != NULL) ? findfunction(GETENTRYNAME(hdr, func), list, number) : NULL;
if (func->address == 0)
{
/* this function is not yet located */
if (funcptr != NULL)
((AMX_FUNCWIDE*)func)->address = funcptr;
else
err = AMX_ERR_NOTFOUND;
}
else if (funcptr != NULL && PawnManager::Get()->core)
{
PawnManager::Get()->core->logLn(LogLevel::Warning, "Tried to register native which is already registered: %s", GETENTRYNAME(hdr, func));
} /* if */
func = (AMX_FUNCPART*)((unsigned char*)func + hdr->defsize);
} /* for */
if (err == AMX_ERR_NONE)
amx->flags |= AMX_FLAG_NTVREG;
return err;
}
5 changes: 3 additions & 2 deletions Server/Components/Pawn/Script/Script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using namespace Impl;
int AMXAPI amx_GetNativeByIndex(AMX const* amx, int index, AMX_NATIVE_INFO* ret);
int AMXAPI amx_MakeAddr(AMX* amx, cell* phys_addr, cell* amx_addr);
int AMXAPI amx_StrSize(const cell* cstr, int* length);
int AMXAPI amx_RegisterChecked(AMX* amx, const AMX_NATIVE_INFO* list, int number);

enum DefaultReturnValue
{
Expand Down Expand Up @@ -81,12 +82,12 @@ class PawnScript
inline int PushArray(cell* amx_addr, cell** phys_addr, const cell array[], int numcells) { return amx_PushArray(&amx_, amx_addr, phys_addr, array, numcells); }
inline int PushString(cell* amx_addr, cell** phys_addr, StringView string, bool pack, bool use_wchar) { return amx_PushStringLen(&amx_, amx_addr, phys_addr, string.data(), string.length(), pack, use_wchar); }
inline int RaiseError(int error) { return amx_RaiseError(&amx_, error); }
inline int Register(const AMX_NATIVE_INFO* nativelist, int number) { return amx_Register(&amx_, nativelist, number); }
inline int Register(const AMX_NATIVE_INFO* nativelist, int number) { return amx_RegisterChecked(&amx_, nativelist, number); }
inline int Register(char const _FAR* name, AMX_NATIVE func)
{
AMX_NATIVE_INFO
nativelist = { name, func };
return amx_Register(&amx_, &nativelist, 1);
return amx_RegisterChecked(&amx_, &nativelist, 1);
}
inline int Release(cell amx_addr) { return amx_Release(&amx_, amx_addr); }
inline int SetCallback(AMX_CALLBACK callback) { return amx_SetCallback(&amx_, callback); }
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Pawn/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static StaticArray<void*, NUM_AMX_FUNCS> AMX_FUNCTIONS = {
reinterpret_cast<void*>(&amx_PushArray),
reinterpret_cast<void*>(&amx_PushString),
reinterpret_cast<void*>(&amx_RaiseError),
reinterpret_cast<void*>(&amx_Register),
reinterpret_cast<void*>(&amx_RegisterChecked),
reinterpret_cast<void*>(&amx_Release),
reinterpret_cast<void*>(&amx_SetCallback),
reinterpret_cast<void*>(&amx_SetDebugHook),
Expand Down

0 comments on commit c634f80

Please sign in to comment.