Skip to content

Commit

Permalink
Fix pawn_try_call_native_msg and co. incorrect arguments layout
Browse files Browse the repository at this point in the history
  • Loading branch information
IS4Code committed Dec 29, 2023
1 parent b458d81 commit 9b5b9c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
22 changes: 11 additions & 11 deletions plugins/src/natives/namx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "utils/shared_id_set_pool.h"
#include <limits>

cell pawn_call(AMX *amx, cell paramsize, cell *params, bool native, bool try_, std::string *msg, AMX *target_amx);
cell pawn_call(AMX *amx, cell paramsize, cell *params, size_t args_start, bool native, bool try_, std::string *msg, AMX *target_amx);
AMX *source_amx;

namespace Natives
Expand Down Expand Up @@ -54,30 +54,30 @@ namespace Natives
return strings::create(amx::load_lock(amx)->name);
}

cell amx_call(AMX *amx, cell *params, bool native, bool try_, std::string *msg)
cell amx_call(AMX *amx, cell *params, size_t args_start, bool native, bool try_, std::string *msg)
{
cell result = 0;
switch(params[1])
{
case -1:
amx::call_all([&](AMX *target_amx)
{
result = pawn_call(amx, params[0] - sizeof(cell), params + 2, native, try_, msg, target_amx);
result = pawn_call(amx, params[0] - sizeof(cell), params + 2, args_start - 1, native, try_, msg, target_amx);
});
break;
case -2:
amx::call_all([&](AMX *target_amx)
{
if(amx != target_amx)
{
result = pawn_call(amx, params[0] - sizeof(cell), params + 2, native, try_, msg, target_amx);
result = pawn_call(amx, params[0] - sizeof(cell), params + 2, args_start - 1, native, try_, msg, target_amx);
}
});
break;
default:
auto target_amx = reinterpret_cast<AMX*>(params[1]);
if(!amx::valid(target_amx)) amx_LogicError(errors::pointer_invalid, "AMX", params[1]);
result = pawn_call(amx, params[0] - sizeof(cell), params + 2, native, try_, msg, target_amx);
result = pawn_call(amx, params[0] - sizeof(cell), params + 2, args_start - 1, native, try_, msg, target_amx);
break;
}
return result;
Expand All @@ -86,27 +86,27 @@ namespace Natives
// native amx_call_native(Amx:amx, const function[], const format[], AnyTag:...);
AMX_DEFINE_NATIVE(amx_call_native, 3)
{
return amx_call(amx, params, true, false, nullptr);
return amx_call(amx, params, 3, true, false, nullptr);
}

// native amx_call_public(Amx:amx, const function[], const format[], AnyTag:...);
AMX_DEFINE_NATIVE_TAG(amx_call_public, 3, cell)
{
return amx_call(amx, params, false, false, nullptr);
return amx_call(amx, params, 3, false, false, nullptr);
}

// native amx_err:amx_try_call_native(Amx:amx, const function[], &result, const format[], AnyTag:...);
AMX_DEFINE_NATIVE_TAG(amx_try_call_native, 4, cell)
{
return amx_call(amx, params, true, true, nullptr);
return amx_call(amx, params, 4, true, true, nullptr);
}

// native amx_err:amx_try_call_native_msg(Amx:amx, const function[], &result, msg[], msg_size=sizeof(msg), const format[]="", AnyTag:...);
AMX_DEFINE_NATIVE_TAG(amx_try_call_native_msg, 5, cell)
{
std::string msg;
cell *msg_addr = amx_GetAddrSafe(amx, params[4]);
cell result = amx_call(amx, params, true, true, &msg);
cell result = amx_call(amx, params, 6, true, true, &msg);
if(msg.size() == 0)
{
amx_SetString(msg_addr, amx::StrError(result), false, false, params[5]);
Expand All @@ -121,7 +121,7 @@ namespace Natives
{
std::string msg;
cell *msg_addr = amx_GetAddrSafe(amx, params[4]);
cell result = amx_call(amx, params, true, true, &msg);
cell result = amx_call(amx, params, 5, true, true, &msg);
if(msg.size() == 0)
{
*msg_addr = strings::create(amx::StrError(result));
Expand All @@ -134,7 +134,7 @@ namespace Natives
// native amx_err:amx_try_call_public(Amx:amx, const function[], &result, const format[], AnyTag:...);
AMX_DEFINE_NATIVE_TAG(amx_try_call_public, 4, cell)
{
return amx_call(amx, params, false, true, nullptr);
return amx_call(amx, params, 4, false, true, nullptr);
}

// native amx_name_length();
Expand Down
20 changes: 9 additions & 11 deletions plugins/src/natives/pawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ struct amx_stack
}
};

cell pawn_call(AMX *amx, cell paramsize, cell *params, bool native, bool try_, std::string *msg, AMX *target_amx)
cell pawn_call(AMX *amx, cell paramsize, cell *params, size_t args_start, bool native, bool try_, std::string *msg, AMX *target_amx)
{
size_t args_start = try_ ? 3 : 2;

char *fname;
amx_StrParam(amx, params[0], fname);

Expand All @@ -84,9 +82,9 @@ cell pawn_call(AMX *amx, cell paramsize, cell *params, bool native, bool try_, s
numargs--;
}

if(paramsize < (2 + numargs) * static_cast<int>(sizeof(cell)))
if(static_cast<size_t>(paramsize) < (args_start + numargs) * static_cast<int>(sizeof(cell)))
{
throw errors::end_of_arguments_error(params, 2 + numargs);
throw errors::end_of_arguments_error(params, args_start + numargs);
}

int pubindex = 0;
Expand Down Expand Up @@ -365,27 +363,27 @@ namespace Natives
// native pawn_call_native(const function[], const format[], AnyTag:...);
AMX_DEFINE_NATIVE(pawn_call_native, 2)
{
return pawn_call(amx, params[0], params + 1, true, false, nullptr, amx);
return pawn_call(amx, params[0], params + 1, 2, true, false, nullptr, amx);
}

// native pawn_call_public(const function[], const format[], AnyTag:...);
AMX_DEFINE_NATIVE_TAG(pawn_call_public, 2, cell)
{
return pawn_call(amx, params[0], params + 1, false, false, nullptr, amx);
return pawn_call(amx, params[0], params + 1, 2 ,false, false, nullptr, amx);
}

// native amx_err:pawn_try_call_native(const function[], &result, const format[], AnyTag:...);
AMX_DEFINE_NATIVE_TAG(pawn_try_call_native, 3, cell)
{
return pawn_call(amx, params[0], params + 1, true, true, nullptr, amx);
return pawn_call(amx, params[0], params + 1, 3, true, true, nullptr, amx);
}

// native amx_err:pawn_try_call_native_msg(const function[], &result, msg[], msg_size=sizeof(msg), const format[]="", AnyTag:...);
AMX_DEFINE_NATIVE_TAG(pawn_try_call_native_msg, 4, cell)
{
std::string msg;
cell *msg_addr = amx_GetAddrSafe(amx, params[3]);
cell result = pawn_call(amx, params[0], params + 1, true, true, &msg, amx);
cell result = pawn_call(amx, params[0], params + 1, 5, true, true, &msg, amx);
if(msg.size() == 0)
{
amx_SetString(msg_addr, amx::StrError(result), false, false, params[4]);
Expand All @@ -400,7 +398,7 @@ namespace Natives
{
std::string msg;
cell *msg_addr = amx_GetAddrSafe(amx, params[3]);
cell result = pawn_call(amx, params[0], params + 1, true, true, &msg, amx);
cell result = pawn_call(amx, params[0], params + 1, 4, true, true, &msg, amx);
if(msg.size() == 0)
{
*msg_addr = strings::create(amx::StrError(result));
Expand All @@ -413,7 +411,7 @@ namespace Natives
// native amx_err:pawn_try_call_public(const function[], &result, const format[], AnyTag:...);
AMX_DEFINE_NATIVE_TAG(pawn_try_call_public, 3, cell)
{
return pawn_call(amx, params[0], params + 1, false, true, nullptr, amx);
return pawn_call(amx, params[0], params + 1, 3, false, true, nullptr, amx);
}

// native pawn_create_callback(const callback[], Expression:action);
Expand Down

0 comments on commit 9b5b9c0

Please sign in to comment.