Skip to content

Commit 7ae77a6

Browse files
committed
Fix SetHamItemInfo string memory handlingFix SetHamItemInfo to store strings in the engine's string pool rather than keeping raw pointers from native parameters. Raw pointers could be overwritten, leading to crashes or undefined behavior.
Native is currently useless without this fix. Applied from upstream repo: alliedmodders#1128
1 parent 48cbfe3 commit 7ae77a6

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

modules/hamsandwich/DataHandler.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ static cell AMX_NATIVE_CALL SetHamItemInfo(AMX *amx, cell *params)
408408
ItemInfo *pItem = reinterpret_cast<ItemInfo *>(params[1]);
409409
cell *ptr = MF_GetAmxAddr(amx, params[3]);
410410
int iLen;
411+
char *szString;
411412

412413
switch (params[2])
413414
{
@@ -420,23 +421,26 @@ static cell AMX_NATIVE_CALL SetHamItemInfo(AMX *amx, cell *params)
420421
break;
421422

422423
case ItemInfo_pszAmmo1:
423-
pItem->pszAmmo1 = MF_GetAmxString(amx, params[3], 0, &iLen);
424+
szString = MF_GetAmxString(amx, params[3], 0, &iLen);
425+
pItem->pszAmmo1 = szString ? STRING(ALLOC_STRING(szString)) : nullptr;
424426
return iLen;
425427

426428
case ItemInfo_iMaxAmmo1:
427429
pItem->iMaxAmmo1 = *ptr;
428430
break;
429431

430432
case ItemInfo_pszAmmo2:
431-
pItem->pszAmmo2 = MF_GetAmxString(amx, params[3], 0, &iLen);
433+
szString = MF_GetAmxString(amx, params[3], 0, &iLen);
434+
pItem->pszAmmo2 = szString ? STRING(ALLOC_STRING(szString)) : nullptr;
432435
return iLen;
433436

434437
case ItemInfo_iMaxAmmo2:
435438
pItem->iMaxAmmo2 = *ptr;
436439
break;
437440

438441
case ItemInfo_pszName:
439-
pItem->pszName = MF_GetAmxString(amx, params[3], 0, &iLen);
442+
szString = MF_GetAmxString(amx, params[3], 0, &iLen);
443+
pItem->pszName = szString ? STRING(ALLOC_STRING(szString)) : nullptr;
440444
return iLen;
441445

442446
case ItemInfo_iMaxClip:

0 commit comments

Comments
 (0)