From 1259ea8e1787a5e9677bcc7cf21338fc81da90f1 Mon Sep 17 00:00:00 2001 From: Emistry Haoyan Date: Thu, 11 Apr 2019 00:13:10 +0800 Subject: Added NULL unit data validation for NPC. - script command `npcspeed`, `npcwalkto`, `npcstop`, `unitwalk`, `unitwarp`, `unitstop` are not applicable for floating npc (without coordinate/location). - Fix map-crash due to unit_data `NULL`. --- src/map/script.c | 108 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 62 insertions(+), 46 deletions(-) diff --git a/src/map/script.c b/src/map/script.c index fe8638ac3..e99547fc7 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -15910,37 +15910,41 @@ static BUILDIN(npctalk) // change npc walkspeed [Valaris] static BUILDIN(npcspeed) { - struct npc_data* nd; - int speed; - - speed = script_getnum(st,2); - nd = map->id2nd(st->oid); + struct npc_data *nd = map->id2nd(st->oid); + int speed = script_getnum(st, 2); if (nd != NULL) { unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit + if (nd->ud == NULL) { + ShowWarning("buildin_npcspeed: floating NPC don't have unit data.\n"); + return false; + } nd->speed = speed; nd->ud->state.speed_changed = 1; } return true; } + // make an npc walk to a position [Valaris] static BUILDIN(npcwalkto) { struct npc_data *nd = map->id2nd(st->oid); - int x=0,y=0; - - x=script_getnum(st,2); - y=script_getnum(st,3); + int x = script_getnum(st, 2); + int y = script_getnum(st, 3); if (nd != NULL) { unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit + if (nd->ud == NULL) { + ShowWarning("buildin_npcwalkto: floating NPC don't have unit data.\n"); + return false; + } if (!nd->status.hp) { status_calc_npc(nd, SCO_FIRST); } else { status_calc_npc(nd, SCO_NONE); } - unit->walktoxy(&nd->bl,x,y,0); + unit->walktoxy(&nd->bl, x, y, 0); } return true; @@ -15952,6 +15956,10 @@ static BUILDIN(npcstop) if (nd != NULL) { unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit + if (nd->ud == NULL) { + ShowWarning("buildin_npcstop: floating NPC don't have unit data.\n"); + return false; + } unit->stop_walking(&nd->bl, STOPWALKING_FLAG_FIXPOS|STOPWALKING_FLAG_NEXTCELL); } @@ -20372,24 +20380,28 @@ static BUILDIN(setunitname) /// unitwalk(,) -> static BUILDIN(unitwalk) { - struct block_list* bl; + struct block_list *bl = map->id2bl(script_getnum(st, 2)); - bl = map->id2bl(script_getnum(st,2)); - if( bl == NULL ) { + if (bl == NULL) { script_pushint(st, 0); return true; } - if( bl->type == BL_NPC ) { - unit->bl2ud2(bl); // ensure the ((struct npc_data*)bl)->ud is safe to edit + if (bl->type == BL_NPC) { + struct unit_data *ud = unit->bl2ud2(bl); // ensure the ((struct npc_data*)bl)->ud is safe to edit + if (ud == NULL) { + ShowWarning("buildin_unitwalk: floating NPC don't have unit data.\n"); + return false; + } } - if( script_hasdata(st,4) ) { - int x = script_getnum(st,3); - int y = script_getnum(st,4); - script_pushint(st, unit->walktoxy(bl,x,y,0));// We'll use harder calculations. - } else { - int target_id = script_getnum(st,3); - script_pushint(st, unit->walktobl(bl,map->id2bl(target_id),1,1)); + if (script_hasdata(st, 4)) { + int x = script_getnum(st, 3); + int y = script_getnum(st, 4); + script_pushint(st, unit->walktoxy(bl, x, y, 0));// We'll use harder calculations. + } + else { + int target_id = script_getnum(st, 3); + script_pushint(st, unit->walktobl(bl, map->id2bl(target_id), 1, 1)); } return true; @@ -20413,32 +20425,34 @@ static BUILDIN(unitkill) /// unitwarp(,"",,) -> static BUILDIN(unitwarp) { - int unit_id; + int unit_id = script_getnum(st, 2); + const char *mapname = script_getstr(st, 3); + short x = (short)script_getnum(st, 4); + short y = (short)script_getnum(st, 5); int mapid; - short x; - short y; - struct block_list* bl; - const char *mapname; - - unit_id = script_getnum(st,2); - mapname = script_getstr(st, 3); - x = (short)script_getnum(st,4); - y = (short)script_getnum(st,5); + struct block_list *bl; if (!unit_id) //Warp the script's runner bl = map->id2bl(st->rid); else bl = map->id2bl(unit_id); - if( strcmp(mapname,"this") == 0 ) - mapid = bl?bl->m:-1; + if (strcmp(mapname, "this") == 0) + mapid = bl ? bl->m : -1; else mapid = map->mapname2mapid(mapname); - if( mapid >= 0 && bl != NULL ) { - unit->bl2ud2(bl); // ensure ((struct npc_data *)bl)->ud is safe to edit - script_pushint(st, unit->warp(bl,mapid,x,y,CLR_OUTSIGHT)); - } else { + if (mapid >= 0 && bl != NULL) { + struct unit_data *ud = unit->bl2ud2(bl); // ensure ((struct npc_data *)bl)->ud is safe to edit + if (bl->type == BL_NPC) { + if (ud == NULL) { + ShowWarning("buildin_unitwarp: floating NPC don't have unit data.\n"); + return false; + } + } + script_pushint(st, unit->warp(bl, mapid, x, y, CLR_OUTSIGHT)); + } + else { script_pushint(st, 0); } @@ -20509,17 +20523,19 @@ static BUILDIN(unitattack) /// unitstop ; static BUILDIN(unitstop) { - int unit_id; - struct block_list* bl; - - unit_id = script_getnum(st,2); + struct block_list *bl = map->id2bl(script_getnum(st, 2)); - bl = map->id2bl(unit_id); - if( bl != NULL ) { - unit->bl2ud2(bl); // ensure ((struct npc_data *)bl)->ud is safe to edit + if (bl != NULL) { + struct unit_data *ud = unit->bl2ud2(bl); // ensure ((struct npc_data *)bl)->ud is safe to edit + if (bl->type == BL_NPC) { + if (ud == NULL) { + ShowWarning("buildin_unitstop: floating NPC don't have unit data.\n"); + return false; + } + } unit->stop_attack(bl); unit->stop_walking(bl, STOPWALKING_FLAG_NEXTCELL); - if( bl->type == BL_MOB ) + if (bl->type == BL_MOB) BL_UCAST(BL_MOB, bl)->target_id = 0; } -- cgit v1.2.3-60-g2f50