From 1bb03e1549a9050049bc027566a448a530298b64 Mon Sep 17 00:00:00 2001 From: FlavioJS Date: Sat, 22 Nov 2008 23:54:34 +0000 Subject: * Hunted down improper uses of va_list variables. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@13383 54d463be-8e91-2dee-dedb-b68131a5f0ec --- Changelog-Trunk.txt | 1 + src/common/showmsg.c | 14 +++++--- src/common/strlib.c | 3 ++ src/map/atcommand.c | 90 +++++++++++++++++++--------------------------------- src/map/clif.c | 2 -- src/map/itemdb.c | 48 ++++++++++++---------------- src/map/map.c | 87 ++++++++++++++++++++++++++++---------------------- src/map/mob.c | 5 --- src/map/npc.c | 4 --- src/map/party.c | 11 ++++--- src/map/skill.c | 4 --- 11 files changed, 122 insertions(+), 147 deletions(-) diff --git a/Changelog-Trunk.txt b/Changelog-Trunk.txt index ec33c8c49..7f77e45ef 100644 --- a/Changelog-Trunk.txt +++ b/Changelog-Trunk.txt @@ -4,6 +4,7 @@ AS OF SVN REV. 5091, WE ARE NOW USING TRUNK. ALL UNTESTED BUGFIXES/FEATURES GO IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK. 2008/11/22 + * Hunted down improper uses of va_list variables. * Hunted down the simpler 64bit pointer truncations. [FlavioJS] 2008/11/18 * Rev. 13375 Autotrade characters will no longer get caught by Urgent Recall. (bugreport:2447) [L0ne_W0lf] diff --git a/src/common/showmsg.c b/src/common/showmsg.c index c50716bed..fc1badd26 100644 --- a/src/common/showmsg.c +++ b/src/common/showmsg.c @@ -673,6 +673,7 @@ char timestamp_format[20] = ""; //For displaying Timestamps int _vShowMessage(enum msg_type flag, const char *string, va_list ap) { + va_list apcopy; char prefix[100]; #if defined(DEBUGLOGMAP) || defined(DEBUGLOGCHAR) || defined(DEBUGLOGLOGIN) FILE *fp; @@ -734,12 +735,16 @@ int _vShowMessage(enum msg_type flag, const char *string, va_list ap) if (flag == MSG_ERROR || flag == MSG_FATALERROR || flag == MSG_SQL) { //Send Errors to StdErr [Skotlex] FPRINTF(STDERR, "%s ", prefix); - VFPRINTF(STDERR, string, ap); + va_copy(apcopy, ap); + VFPRINTF(STDERR, string, apcopy); + va_end(apcopy); FFLUSH(STDERR); } else { if (flag != MSG_NONE) FPRINTF(STDOUT, "%s ", prefix); - VFPRINTF(STDOUT, string, ap); + va_copy(apcopy, ap); + VFPRINTF(STDOUT, string, apcopy); + va_end(apcopy); FFLUSH(STDOUT); } @@ -751,7 +756,9 @@ int _vShowMessage(enum msg_type flag, const char *string, va_list ap) FFLUSH(STDERR); } else { fprintf(fp,"%s ", prefix); - vfprintf(fp,string,ap); + va_copy(apcopy, ap); + vfprintf(fp,string,apcopy); + va_end(apcopy); fclose(fp); } } else { @@ -760,7 +767,6 @@ int _vShowMessage(enum msg_type flag, const char *string, va_list ap) } #endif - va_end(ap); return 0; } diff --git a/src/common/strlib.c b/src/common/strlib.c index 93b69ab54..5580124e1 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -964,9 +964,12 @@ int StringBuf_Vprintf(StringBuf* self, const char* fmt, va_list ap) for(;;) { + va_list apcopy; /* Try to print in the allocated space. */ size = self->max_ - (self->ptr_ - self->buf_); + va_copy(apcopy, ap); n = vsnprintf(self->ptr_, size, fmt, ap); + va_end(ap); /* If that worked, return the length. */ if( n > -1 && n < size ) { diff --git a/src/map/atcommand.c b/src/map/atcommand.c index 48e9a0720..f3cca853a 100644 --- a/src/map/atcommand.c +++ b/src/map/atcommand.c @@ -2561,7 +2561,6 @@ static int atkillmonster_sub(struct block_list *bl, va_list ap) struct mob_data *md; int flag; - nullpo_retr(0, ap); nullpo_retr(0, md=(struct mob_data *)bl); flag = va_arg(ap, int); @@ -6311,35 +6310,12 @@ int atcommand_sound(const int fd, struct map_session_data *sd, const char *comma /*========================================== * MOB Search *------------------------------------------*/ -static int atmobsearch_sub(struct block_list *bl,va_list ap) -{ - int mob_id,fd; - static int number=0; - struct mob_data *md; - - nullpo_retr(0, bl); - - if(!ap){ - number=0; - return 0; - } - mob_id = va_arg(ap,int); - fd = va_arg(ap,int); - - md = (struct mob_data *)bl; - - if(md && fd && (mob_id==-1 || (md->class_==mob_id))){ - snprintf(atcmd_output, sizeof atcmd_output, "%2d[%3d:%3d] %s", - ++number,bl->x, bl->y,md->name); - clif_displaymessage(fd, atcmd_output); - } - return 0; -} - int atcommand_mobsearch(const int fd, struct map_session_data* sd, const char* command, const char* message) { char mob_name[100]; int mob_id,map_id = 0; + int number = 0; + struct s_mapiterator* it; nullpo_retr(-1, sd); @@ -6364,9 +6340,21 @@ int atcommand_mobsearch(const int fd, struct map_session_data* sd, const char* c snprintf(atcmd_output, sizeof atcmd_output, "Mob Search... %s %s", mob_name, mapindex_id2name(sd->mapindex)); clif_displaymessage(fd, atcmd_output); - map_foreachinmap(atmobsearch_sub, map_id, BL_MOB, mob_id, fd); + it = mapit_geteachmob(); + while( true ) + { + TBL_MOB* md = (TBL_MOB*)mapit_next(it); + if( md == NULL ) + break;// no more mobs - atmobsearch_sub(&sd->bl,0); // reset the counter + if( mob_id == -1 || md->class_ == mob_id ) + { + ++number; + snprintf(atcmd_output, sizeof atcmd_output, "%2d[%3d:%3d] %s", number, md->bl.x, md->bl.y, md->name); + clif_displaymessage(fd, atcmd_output); + } + } + mapit_free(it); return 0; } @@ -7021,36 +7009,12 @@ int atshowmobs_timer(int tid, unsigned int tick, int id, intptr data) return 1; } -static int atshowmobs_sub(struct block_list *bl,va_list ap) -{ - int mob_id; - struct map_session_data* sd; - static int number=0; - struct mob_data *md; - - if(!ap){ - number=0; - return 0; - } - mob_id = va_arg(ap,int); - sd = va_arg(ap,struct map_session_data*); - - md = (struct mob_data *)bl; - - if(md->special_state.ai || md->master_id) - return 0; //Hide slaves and player summoned mobs. [Skotlex] - - if(mob_id==-1 || md->class_==mob_id){ - clif_viewpoint(sd, 1, 1, bl->x, bl->y, ++number, 0xFFFFFF); - add_timer(gettick()+5000, atshowmobs_timer, sd->bl.id, number); - } - return 0; -} - int atcommand_showmobs(const int fd, struct map_session_data* sd, const char* command, const char* message) { char mob_name[100]; int mob_id,map_id = 0; + int number = 0; + struct s_mapiterator* it; nullpo_retr(-1, sd); @@ -7081,11 +7045,23 @@ int atcommand_showmobs(const int fd, struct map_session_data* sd, const char* co snprintf(atcmd_output, sizeof atcmd_output, "Mob Search... %s %s", mob_name, mapindex_id2name(sd->mapindex)); - clif_displaymessage(fd, atcmd_output); + clif_displaymessage(fd, atcmd_output); - map_foreachinmap(atshowmobs_sub, map_id, BL_MOB, mob_id, sd); + it = mapit_geteachmob(); + while( true ) + { + TBL_MOB* md = (TBL_MOB*)mapit_next(it); + if( md == NULL ) + break;// no more mobs - atshowmobs_sub(&sd->bl,0); + if( mob_id == -1 || md->class_ == mob_id ) + { + ++number; + clif_viewpoint(sd, 1, 1, md->bl.x, md->bl.y, number, 0xFFFFFF); + add_timer(gettick()+5000, atshowmobs_timer, sd->bl.id, number); + } + } + mapit_free(it); return 0; } diff --git a/src/map/clif.c b/src/map/clif.c index 46e9c0fb9..8e9ec4cf9 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -171,7 +171,6 @@ int clif_send_sub(struct block_list *bl, va_list ap) int len, type, fd; nullpo_retr(0, bl); - nullpo_retr(0, ap); nullpo_retr(0, sd = (struct map_session_data *)bl); fd = sd->fd; @@ -3718,7 +3717,6 @@ void clif_01ac(struct block_list* bl) struct map_session_data *sd; nullpo_retr(0, bl); - nullpo_retr(0, ap); sd=va_arg(ap,struct map_session_data*); diff --git a/src/map/itemdb.c b/src/map/itemdb.c index 3bcd07cf1..1cc7a5cb4 100644 --- a/src/map/itemdb.c +++ b/src/map/itemdb.c @@ -255,13 +255,13 @@ static void create_dummy_data(void) dummy_item.view_id=UNKNOWN_ITEM_ID; } -static void* create_item_data(DBKey key, va_list args) +static struct item_data* create_item_data(int nameid) { struct item_data *id; CREATE(id, struct item_data, 1); - id->nameid=key.i; - id->weight=1; - id->type=IT_ETC; + id->nameid = nameid; + id->weight = 1; + id->type = IT_ETC; return id; } @@ -271,50 +271,42 @@ static void* create_item_data(DBKey key, va_list args) struct item_data* itemdb_load(int nameid) { struct item_data *id; - DBKey key; if( nameid >= 0 && nameid < ARRAYLENGTH(itemdb_array) ) { id = itemdb_array[nameid]; - if( id == NULL ) - { - key.i = nameid; - id = itemdb_array[nameid] = (struct item_data*)create_item_data(key, NULL); - } + if( id == NULL || id == &dummy_item ) + id = itemdb_array[nameid] = create_item_data(nameid); return id; } - id = (struct item_data*)idb_ensure(itemdb_other, nameid, create_item_data); - if( id == &dummy_item ) - {// Remove dummy_item, replace by real data. - key.i = nameid; - id = (struct item_data*)create_item_data(key, NULL); + id = (struct item_data*)idb_get(itemdb_other, nameid); + if( id == NULL || id == &dummy_item ) + { + id = create_item_data(nameid); idb_put(itemdb_other, nameid, id); } return id; } -static void* return_dummy_data(DBKey key, va_list args) -{ - ShowWarning("itemdb_search: Item ID %d does not exists in the item_db. Using dummy data.\n", key.i); - dummy_item.nameid = key.i; - return &dummy_item; -} - /*========================================== * Loads an item from the db. If not found, it will return the dummy item. *------------------------------------------*/ struct item_data* itemdb_search(int nameid) { + struct item_data* id; if( nameid >= 0 && nameid < ARRAYLENGTH(itemdb_array) ) + id = itemdb_array[nameid]; + else + id = (struct item_data*)idb_get(itemdb_other, nameid); + + if( id == NULL ) { - DBKey key; - if( itemdb_array[nameid] ) - return itemdb_array[nameid]; - key.i = nameid; - return (struct item_data*)return_dummy_data(key, NULL); + ShowWarning("itemdb_search: Item ID %d does not exists in the item_db. Using dummy data.\n", nameid); + id = &dummy_item; + dummy_item.nameid = nameid; } - return (struct item_data*)idb_ensure(itemdb_other,nameid,return_dummy_data); + return id; } /*========================================== diff --git a/src/map/map.c b/src/map/map.c index 0312b926f..21239a78b 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -511,13 +511,11 @@ struct skill_unit* map_find_skill_unit_oncell(struct block_list* target,int x,in *------------------------------------------*/ int map_foreachinrange(int (*func)(struct block_list*,va_list), struct block_list* center, int range, int type, ...) { - va_list ap; int bx,by,m; int returnCount =0; //total sum of returned values of func() [Skotlex] struct block_list *bl; int blockcount=bl_list_count,i; int x0,x1,y0,y1; - va_start(ap,type); m = center->m; x0 = max(center->x-range, 0); @@ -562,11 +560,15 @@ int map_foreachinrange(int (*func)(struct block_list*,va_list), struct block_lis for(i=blockcount;iprev) // 有?かどうかチェック - returnCount += func(bl_list[i],ap); + { + va_list ap; + va_start(ap, type); + returnCount += func(bl_list[i], ap); + va_end(ap); + } map_freeblock_unlock(); // 解放を許可する - va_end(ap); bl_list_count = blockcount; return returnCount; //[Skotlex] } @@ -576,7 +578,6 @@ int map_foreachinrange(int (*func)(struct block_list*,va_list), struct block_lis *------------------------------------------*/ int map_foreachinshootrange(int (*func)(struct block_list*,va_list),struct block_list* center, int range, int type,...) { - va_list ap; int bx,by,m; int returnCount =0; //total sum of returned values of func() [Skotlex] struct block_list *bl; @@ -587,8 +588,6 @@ int map_foreachinshootrange(int (*func)(struct block_list*,va_list),struct block if (m < 0) return 0; - va_start(ap,type); - x0 = max(center->x-range, 0); y0 = max(center->y-range, 0); x1 = min(center->x+range, map[m].xs-1); @@ -633,11 +632,15 @@ int map_foreachinshootrange(int (*func)(struct block_list*,va_list),struct block for(i=blockcount;iprev) // 有?かどうかチェック - returnCount += func(bl_list[i],ap); + { + va_list ap; + va_start(ap, type); + returnCount += func(bl_list[i], ap); + va_end(ap); + } map_freeblock_unlock(); // 解放を許可する - va_end(ap); bl_list_count = blockcount; return returnCount; //[Skotlex] } @@ -649,7 +652,6 @@ int map_foreachinshootrange(int (*func)(struct block_list*,va_list),struct block *------------------------------------------*/ int map_foreachinarea(int (*func)(struct block_list*,va_list), int m, int x0, int y0, int x1, int y1, int type, ...) { - va_list ap; int bx,by; int returnCount =0; //total sum of returned values of func() [Skotlex] struct block_list *bl; @@ -657,7 +659,6 @@ int map_foreachinarea(int (*func)(struct block_list*,va_list), int m, int x0, in if (m < 0) return 0; - va_start(ap,type); if (x1 < x0) { //Swap range bx = x0; @@ -696,11 +697,15 @@ int map_foreachinarea(int (*func)(struct block_list*,va_list), int m, int x0, in for(i=blockcount;iprev) // 有?かどうかチェック - returnCount += func(bl_list[i],ap); + { + va_list ap; + va_start(ap, type); + returnCount += func(bl_list[i], ap); + va_end(ap); + } map_freeblock_unlock(); // 解放を許可する - va_end(ap); bl_list_count = blockcount; return returnCount; //[Skotlex] } @@ -717,13 +722,11 @@ int map_foreachinmovearea(int (*func)(struct block_list*,va_list), struct block_ int bx,by,m; int returnCount =0; //total sum of returned values of func() [Skotlex] struct block_list *bl; - va_list ap; int blockcount=bl_list_count,i; int x0, x1, y0, y1; if (!range) return 0; if (!dx && !dy) return 0; //No movement. - va_start(ap,type); m = center->m; x0 = center->x-range; @@ -830,11 +833,15 @@ int map_foreachinmovearea(int (*func)(struct block_list*,va_list), struct block_ for(i=blockcount;iprev) - returnCount += func(bl_list[i],ap); + { + va_list ap; + va_start(ap, type); + returnCount += func(bl_list[i], ap); + va_end(ap); + } map_freeblock_unlock(); // 解放を許可する - va_end(ap); bl_list_count = blockcount; return returnCount; } @@ -848,13 +855,10 @@ int map_foreachincell(int (*func)(struct block_list*,va_list), int m, int x, int int bx,by; int returnCount =0; //total sum of returned values of func() [Skotlex] struct block_list *bl; - va_list ap; int blockcount=bl_list_count,i; if (x < 0 || y < 0 || x >= map[m].xs || y >= map[m].ys) return 0; - va_start(ap,type); - by=y/BLOCK_SIZE; bx=x/BLOCK_SIZE; @@ -875,11 +879,15 @@ int map_foreachincell(int (*func)(struct block_list*,va_list), int m, int x, int for(i=blockcount;iprev) // 有?かどうかチェック - returnCount += func(bl_list[i],ap); + { + va_list ap; + va_start(ap, type); + returnCount += func(bl_list[i], ap); + va_end(ap); + } map_freeblock_unlock(); // 解放を許可する - va_end(ap); bl_list_count = blockcount; return returnCount; } @@ -924,7 +932,6 @@ int map_foreachinpath(int (*func)(struct block_list*,va_list),int m,int x0,int y // kRO. //Generic map_foreach* variables. - va_list ap; int i, blockcount = bl_list_count; struct block_list *bl; int bx, by; @@ -938,8 +945,6 @@ int map_foreachinpath(int (*func)(struct block_list*,va_list),int m,int x0,int y if (m < 0) return 0; - - va_start(ap,type); len_limit = magnitude2 = MAGNITUDE2(x0,y0, x1,y1); if (magnitude2 < 1) //Same begin and ending point, can't trace path. @@ -1068,11 +1073,15 @@ int map_foreachinpath(int (*func)(struct block_list*,va_list),int m,int x0,int y for(i=blockcount;iprev) //This check is done in case some object gets killed due to further skill processing. - returnCount += func(bl_list[i],ap); + { + va_list ap; + va_start(ap, type); + returnCount += func(bl_list[i], ap); + va_end(ap); + } map_freeblock_unlock(); - va_end(ap); bl_list_count = blockcount; return returnCount; //[Skotlex] @@ -1084,11 +1093,8 @@ int map_foreachinmap(int (*func)(struct block_list*,va_list), int m, int type,.. int b, bsize; int returnCount =0; //total sum of returned values of func() [Skotlex] struct block_list *bl; - va_list ap; int blockcount=bl_list_count,i; - va_start(ap,type); - bsize = map[m].bxs * map[m].bys; if(type&~BL_MOB) @@ -1110,11 +1116,15 @@ int map_foreachinmap(int (*func)(struct block_list*,va_list), int m, int type,.. for(i=blockcount;iprev) // 有?かどうかチェック - returnCount += func(bl_list[i],ap); + { + va_list ap; + va_start(ap, type); + returnCount += func(bl_list[i], ap); + va_end(ap); + } map_freeblock_unlock(); // 解放を許可する - va_end(ap); bl_list_count = blockcount; return returnCount; } @@ -1210,9 +1220,6 @@ void map_foreachobject(int (*func)(struct block_list*,va_list),int type,...) { int i; int blockcount=bl_list_count; - va_list ap; - - va_start(ap,type); for(i=2;i<=last_object_id;i++){ if(objects[i]){ @@ -1230,11 +1237,15 @@ void map_foreachobject(int (*func)(struct block_list*,va_list),int type,...) for(i=blockcount;iprev || bl_list[i]->next ) - func(bl_list[i],ap); + { + va_list ap; + va_start(ap, type); + func(bl_list[i], ap); + va_end(ap); + } map_freeblock_unlock(); - va_end(ap); bl_list_count = blockcount; } @@ -3271,7 +3282,7 @@ int cleanup_sub(struct block_list *bl, va_list ap) static int cleanup_db_sub(DBKey key,void *data,va_list va) { - return cleanup_sub((struct block_list*)data, NULL); + return cleanup_sub((struct block_list*)data, va); } /*========================================== diff --git a/src/map/mob.c b/src/map/mob.c index de1ad7294..8b522548f 100644 --- a/src/map/mob.c +++ b/src/map/mob.c @@ -697,7 +697,6 @@ int mob_linksearch(struct block_list *bl,va_list ap) unsigned int tick; nullpo_retr(0, bl); - nullpo_retr(0, ap); md=(struct mob_data *)bl; class_ = va_arg(ap, int); target = va_arg(ap, struct block_list *); @@ -898,7 +897,6 @@ static int mob_ai_sub_hard_activesearch(struct block_list *bl,va_list ap) int dist; nullpo_retr(0, bl); - nullpo_retr(0, ap); md=va_arg(ap,struct mob_data *); target= va_arg(ap,struct block_list**); @@ -946,7 +944,6 @@ static int mob_ai_sub_hard_changechase(struct block_list *bl,va_list ap) struct block_list **target; nullpo_retr(0, bl); - nullpo_retr(0, ap); md=va_arg(ap,struct mob_data *); target= va_arg(ap,struct block_list**); @@ -1708,7 +1705,6 @@ int mob_deleteslave_sub(struct block_list *bl,va_list ap) int id; nullpo_retr(0, bl); - nullpo_retr(0, ap); nullpo_retr(0, md = (struct mob_data *)bl); id=va_arg(ap,int); @@ -2771,7 +2767,6 @@ int mob_getfriendstatus_sub(struct block_list *bl,va_list ap) int flag=0; nullpo_retr(0, bl); - nullpo_retr(0, ap); nullpo_retr(0, md=(struct mob_data *)bl); nullpo_retr(0, mmd=va_arg(ap,struct mob_data *)); diff --git a/src/map/npc.c b/src/map/npc.c index a347cd128..639de90d7 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -92,7 +92,6 @@ int npc_enable_sub(struct block_list *bl, va_list ap) struct npc_data *nd; nullpo_retr(0, bl); - nullpo_retr(0, ap); nullpo_retr(0, nd=va_arg(ap,struct npc_data *)); if(bl->type == BL_PC && (sd=(struct map_session_data *)bl)) { @@ -232,7 +231,6 @@ int npc_event_doall_sub(DBKey key, void* data, va_list ap) int rid; nullpo_retr(0, ev = (struct event_data *)data); - nullpo_retr(0, ap); nullpo_retr(0, c = va_arg(ap, int *)); nullpo_retr(0, name = va_arg(ap, const char *)); rid = va_arg(ap, int); @@ -258,7 +256,6 @@ static int npc_event_do_sub(DBKey key, void* data, va_list ap) const char* name; nullpo_retr(0, ev = (struct event_data *)data); - nullpo_retr(0, ap); nullpo_retr(0, c = va_arg(ap, int *)); nullpo_retr(0, name = va_arg(ap, const char *)); @@ -1795,7 +1792,6 @@ int npc_convertlabel_db(DBKey key, void* data, va_list ap) const char *p; int len; - nullpo_retr(0, ap); nullpo_retr(0, label_list = va_arg(ap,struct npc_label_list**)); nullpo_retr(0, label_list_num = va_arg(ap,int*)); nullpo_retr(0, filepath = va_arg(ap,const char*)); diff --git a/src/map/party.c b/src/map/party.c index a6f2c454f..46ddf2952 100644 --- a/src/map/party.c +++ b/src/map/party.c @@ -929,7 +929,6 @@ int party_sub_count(struct block_list *bl, va_list ap) int party_foreachsamemap(int (*func)(struct block_list*,va_list),struct map_session_data *sd,int range,...) { struct party_data *p; - va_list ap; int i; int x0,y0,x1,y1; struct block_list *list[MAX_PARTY]; @@ -946,8 +945,6 @@ int party_foreachsamemap(int (*func)(struct block_list*,va_list),struct map_sess x1=sd->bl.x+range; y1=sd->bl.y+range; - va_start(ap,range); - for(i=0;idata[i].sd; @@ -964,10 +961,14 @@ int party_foreachsamemap(int (*func)(struct block_list*,va_list),struct map_sess map_freeblock_lock(); for(i=0;itype == BL_PC && (sd=(struct map_session_data *)src) && bl->type==BL_ITEM && (fitem=(struct flooritem_data *)bl)) -- cgit v1.2.3-60-g2f50