Skip to content

Commit

Permalink
Fix rg_set_iteminfo allocate string via engine
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lentq committed Feb 11, 2018
1 parent abd40fd commit 9e41f5f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 7 additions & 0 deletions reapi/src/natives/natives_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ const char* getAmxString(AMX* amx, cell addr, char (&dest)[N], size_t* len = nul
return getAmxString(getAmxAddr(amx, addr), dest, N - 1, len);
}

template<size_t N>
string_t getAmxStringAlloc(AMX* amx, cell addr, char (&dest)[N], size_t* len = nullptr)
{
const char* pszDest = getAmxString(getAmxAddr(amx, addr), dest, N - 1, len);
return (pszDest && pszDest[0] != '\0') ? ALLOC_STRING(pszDest) : iStringNull;
}

inline void fillNatives(AMX_NATIVE_INFO* table, cell (AMX_NATIVE_CALL with)(AMX *, cell *))
{
for (size_t i = 0; table[i].name; i++)
Expand Down
8 changes: 4 additions & 4 deletions reapi/src/natives/natives_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ cell AMX_NATIVE_CALL rg_create_entity(AMX *amx, cell *params)
enum args_e { arg_count, arg_classname, arg_hashtable };

char classname[256];
string_t iClass = g_engfuncs.pfnAllocString(getAmxString(amx, params[arg_classname], classname));
string_t iClass = getAmxStringAlloc(amx, params[arg_classname], classname);

edict_t *pEntity;
if (params[arg_hashtable] != 0)
Expand Down Expand Up @@ -1993,9 +1993,9 @@ cell AMX_NATIVE_CALL rg_set_iteminfo(AMX *amx, cell *params)
case ItemInfo_iId: pItem->m_ItemInfo.iId = *ptr; break;
case ItemInfo_iFlags: pItem->m_ItemInfo.iFlags = *ptr; break;
case ItemInfo_iWeight: pItem->m_ItemInfo.iWeight = *ptr; break;
case ItemInfo_pszAmmo1: pItem->m_ItemInfo.pszAmmo1 = getAmxString(amx, params[arg_value], itembuf); break;
case ItemInfo_pszAmmo2: pItem->m_ItemInfo.pszAmmo2 = getAmxString(amx, params[arg_value], itembuf); break;
case ItemInfo_pszName: pItem->m_ItemInfo.pszName = getAmxString(amx, params[arg_value], itembuf); break;
case ItemInfo_pszAmmo1: pItem->m_ItemInfo.pszAmmo1 = STRING(getAmxStringAlloc(amx, params[arg_value], itembuf)); break;
case ItemInfo_pszAmmo2: pItem->m_ItemInfo.pszAmmo2 = STRING(getAmxStringAlloc(amx, params[arg_value], itembuf)); break;
case ItemInfo_pszName: pItem->m_ItemInfo.pszName = STRING(getAmxStringAlloc(amx, params[arg_value], itembuf)); break;

default:
AMXX_LogError(amx, AMX_ERR_NATIVE, "Unknown ItemInfo type %d", type);
Expand Down

0 comments on commit 9e41f5f

Please sign in to comment.