Fixes a crash from script command searchitem (#7446)

This commit is contained in:
Aleos 2022-11-19 08:54:47 -05:00 committed by GitHub
parent 0e1285a94b
commit 3024d08a39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18412,58 +18412,48 @@ BUILDIN_FUNC(checkidlemer)
BUILDIN_FUNC(searchitem) BUILDIN_FUNC(searchitem)
{ {
struct script_data* data = script_getdata(st, 2); script_data* data = script_getdata(st, 2);
const char *itemname = script_getstr(st,3); const char *name = reference_getname(data);
std::map<t_itemid, std::shared_ptr<item_data>> items;
int count;
char* name;
int32 start;
int32 id;
int32 i;
TBL_PC* sd = NULL;
if ((items[0] = item_db.find(strtoul(itemname, nullptr, 10))))
count = 1;
else
count = itemdb_searchname_array(items, MAX_SEARCH, itemname);
if (!count) {
script_pushint(st, 0);
return SCRIPT_CMD_SUCCESS;
}
if( !data_isreference(data) ) if( !data_isreference(data) )
{ {
ShowError("script:searchitem: not a variable\n"); ShowError("buildin_searchitem: Argument %s is not a variable.\n", name);
script_reportdata(data); script_reportdata(data);
st->state = END; st->state = END;
return SCRIPT_CMD_FAILURE;// not a variable return SCRIPT_CMD_FAILURE;// not a variable
} }
id = reference_getid(data); if( is_string_variable(name) )
start = reference_getindex(data); {// string array
name = reference_getname(data); ShowError("buildin_searchitem: Argument %s is not an integer array.\n", name);
script_reportdata(data);
st->state = END;
return SCRIPT_CMD_FAILURE;// not supported
}
map_session_data *sd = nullptr;
if (not_server_variable(*name) && !script_rid2sd(sd)) if (not_server_variable(*name) && !script_rid2sd(sd))
{ {
return SCRIPT_CMD_SUCCESS;// no player attached return SCRIPT_CMD_SUCCESS;// no player attached
} }
if( is_string_variable(name) ) const char *itemname = script_getstr(st, 3);
{// string array std::map<t_itemid, std::shared_ptr<item_data>> items;
ShowError("script:searchitem: not an integer array reference\n");
script_reportdata(data); itemdb_searchname_array(items, MAX_SEARCH, itemname);
st->state = END;
return SCRIPT_CMD_FAILURE;// not supported if (!items.empty()) {
int32 id = reference_getid(data);
int32 start = reference_getindex(data);
for (const auto &it : items) { // Set array
set_reg_num(st, sd, reference_uid(id, start), name, it.first, reference_getref(data));
start++;
}
} }
for( i = 0; i < count; ++start, ++i ) script_pushint64(st, items.size());
{// Set array
set_reg_num( st, sd, reference_uid( id, start ), name, items[i]->nameid, reference_getref( data ) );
}
script_pushint(st, count);
return SCRIPT_CMD_SUCCESS; return SCRIPT_CMD_SUCCESS;
} }