From 03e21b097d226e81951bdf0bffe08bdbd4902371 Mon Sep 17 00:00:00 2001 From: ac-stef Date: Sat, 17 Jun 2017 12:40:35 +0200 Subject: [PATCH] fix stuff found by cppcheck almost 3000 lines of "errors" contained one actual memleak, one actually unused variable and three actual ambiguous code parts from cppcheck website: "The goal is to detect only real errors in the code (i.e. have zero false positives)." :) thx for finding five problems in our code --- source/src/autodownload.cpp | 2 +- source/src/menus.cpp | 2 +- source/src/rendermodel.cpp | 4 +--- source/src/scoreboard.cpp | 2 +- source/src/server.cpp | 6 +++--- source/src/tools.cpp | 2 +- source/src/tools.h | 2 +- source/src/worldio.cpp | 2 +- 8 files changed, 10 insertions(+), 12 deletions(-) diff --git a/source/src/autodownload.cpp b/source/src/autodownload.cpp index f831d817..d2354050 100644 --- a/source/src/autodownload.cpp +++ b/source/src/autodownload.cpp @@ -305,7 +305,7 @@ void processdownload(package *pck, stream *f) // write downloaded data to file(s else { // any name with listed extension, written to subdirectory int zlen = (int)strlen(zname), elen = (int)strlen(pck->exts[n]); - if((isok = zlen > elen && !strcmp(zname + zlen - elen, pck->exts[n]))) formatstring(filename)("%s/%s", pck->fullpath, zname); + if((isok = (zlen > elen && !strcmp(zname + zlen - elen, pck->exts[n])))) formatstring(filename)("%s/%s", pck->fullpath, zname); } if(isok) break; } diff --git a/source/src/menus.cpp b/source/src/menus.cpp index 81f51abc..926cb624 100644 --- a/source/src/menus.cpp +++ b/source/src/menus.cpp @@ -743,7 +743,7 @@ struct mitemcheckbox : mitem { bool sel = isselection(); const static int boxsize = FONTH; - int offs = ((menurighttabwidth - FONTH) * (msctrl % 41 ? pos : 50 + 50 * sinf(totalmillis / 300.0f + y))) / 100; + int offs = ((menurighttabwidth - FONTH) * ((msctrl % 41) ? pos : (50 + 50 * sinf(totalmillis / 300.0f + y)))) / 100; draw_text(text, x, y); if(sel) diff --git a/source/src/rendermodel.cpp b/source/src/rendermodel.cpp index 2a6ade05..78b8851a 100644 --- a/source/src/rendermodel.cpp +++ b/source/src/rendermodel.cpp @@ -956,7 +956,6 @@ bool preload_mapmodels(bool trydl) void renderclient(playerent *d, const char *mdlname, const char *vwepname, int tex) { - int varseed = (int)(size_t)d; int anim = ANIM_IDLE|ANIM_LOOP; float speed = 0.0; vec o(d->o); @@ -968,7 +967,6 @@ void renderclient(playerent *d, const char *mdlname, const char *vwepname, int t loopv(bounceents) if(bounceents[i]->bouncetype==BT_GIB && bounceents[i]->owner==d) return; d->pitch = 0.1f; anim = ANIM_DEATH; - varseed += d->lastpain; basetime = d->lastpain; int t = lastmillis-d->lastpain; if(t<0 || t>20000) return; @@ -982,7 +980,7 @@ void renderclient(playerent *d, const char *mdlname, const char *vwepname, int t } else if(d->state==CS_EDITING) { anim = ANIM_JUMP|ANIM_END; } else if(d->state==CS_LAGGED) { anim = ANIM_SALUTE|ANIM_LOOP|ANIM_TRANSLUCENT; } - else if(lastmillis-d->lastpain<300) { anim = d->crouching ? ANIM_CROUCH_PAIN : ANIM_PAIN; speed = 300.0f/4; varseed += d->lastpain; basetime = d->lastpain; } + else if(lastmillis-d->lastpain<300) { anim = d->crouching ? ANIM_CROUCH_PAIN : ANIM_PAIN; speed = 300.0f/4; basetime = d->lastpain; } else if(d->weaponsel==d->lastattackweapon && lastmillis-d->lastaction<300 && d->lastpain < d->lastaction && !d->weaponsel->reloading && (d->weaponsel->type != GUN_GRENADE || ((grenades *)d->weaponsel)->cookingmillis)) { anim = d->crouching ? ANIM_CROUCH_ATTACK : ANIM_ATTACK; speed = 300.0f/8; basetime = d->lastaction; } else if(!d->onfloor && d->timeinair>50) { anim = (d->crouching ? ANIM_CROUCH_WALK : ANIM_JUMP)|ANIM_END; } diff --git a/source/src/scoreboard.cpp b/source/src/scoreboard.cpp index 7a571f4d..bf5f0fa8 100644 --- a/source/src/scoreboard.cpp +++ b/source/src/scoreboard.cpp @@ -458,7 +458,7 @@ const char *asciiscores(bool destjpg) buf[0] = '\0'; if(destjpg) { - formatstring(text)("AssaultCube Screenshot (%s)\n", asctime()); + formatstring(text)("AssaultCube Screenshot (%s)\n", asctimestr()); addstr(buf, text); } if(getclientmap()[0]) diff --git a/source/src/server.cpp b/source/src/server.cpp index 7290f094..841b1f5e 100644 --- a/source/src/server.cpp +++ b/source/src/server.cpp @@ -669,7 +669,7 @@ void enddemorecord() demofile &d = demofiles.add(); //2010oct10:ft: suggests : formatstring(d.info)("%s, %s, %.2f%s", modestr(gamemode), smapname, len > 1024*1024 ? len/(1024*1024.f) : len/1024.0f, len > 1024*1024 ? "MB" : "kB"); // the datetime bit is pretty useless in the servmesg, no?! - formatstring(d.info)("%s: %s, %s, %.2f%s", asctime(), modestr(gamemode), smapname, len > 1024*1024 ? len/(1024*1024.f) : len/1024.0f, len > 1024*1024 ? "MB" : "kB"); + formatstring(d.info)("%s: %s, %s, %.2f%s", asctimestr(), modestr(gamemode), smapname, len > 1024*1024 ? len/(1024*1024.f) : len/1024.0f, len > 1024*1024 ? "MB" : "kB"); if(mr) { concatformatstring(d.info, ", %d mr", mr); concatformatstring(d.file, "_%dmr", mr); } defformatstring(msg)("Demo \"%s\" recorded\nPress F10 to download it from the server..", d.info); sendservmsg(msg); @@ -740,9 +740,9 @@ void setupdemorecord() lilswap(&hdr.version, 1); lilswap(&hdr.protocol, 1); memset(hdr.desc, 0, DHDR_DESCCHARS); - defformatstring(desc)("%s, %s, %s %s", modestr(gamemode, false), behindpath(smapname), asctime(), servdesc_current); + defformatstring(desc)("%s, %s, %s %s", modestr(gamemode, false), behindpath(smapname), asctimestr(), servdesc_current); if(strlen(desc) > DHDR_DESCCHARS) - formatstring(desc)("%s, %s, %s %s", modestr(gamemode, true), behindpath(smapname), asctime(), servdesc_current); + formatstring(desc)("%s, %s, %s %s", modestr(gamemode, true), behindpath(smapname), asctimestr(), servdesc_current); desc[DHDR_DESCCHARS - 1] = '\0'; strcpy(hdr.desc, desc); memset(hdr.plist, 0, DHDR_PLISTCHARS); diff --git a/source/src/tools.cpp b/source/src/tools.cpp index f1c95c76..a8732def 100644 --- a/source/src/tools.cpp +++ b/source/src/tools.cpp @@ -30,7 +30,7 @@ const char *timestring(time_t t, bool local, const char *fmt, char *buf) return buf; } -const char *asctime() +const char *asctimestr() { return timestring(true, "%c"); } diff --git a/source/src/tools.h b/source/src/tools.h index 0e7e655c..fd688c81 100644 --- a/source/src/tools.h +++ b/source/src/tools.h @@ -924,7 +924,7 @@ struct stream extern string _timestringbuffer; extern const char *timestring(time_t t, bool local = false, const char *fmt = NULL, char *buf = _timestringbuffer); inline const char *timestring(bool local = false, const char *fmt = NULL, char *buf = _timestringbuffer) { return timestring(time(NULL), local, fmt, buf); } -extern const char *asctime(); +extern const char *asctimestr(); extern const char *numtime(); extern void transformoldentitytypes(int mapversion, uchar &enttype); extern int fixmapheadersize(int version, int headersize); diff --git a/source/src/worldio.cpp b/source/src/worldio.cpp index 85701f0b..47cf343b 100644 --- a/source/src/worldio.cpp +++ b/source/src/worldio.cpp @@ -864,7 +864,7 @@ int load_world(char *mname) // still supports all map formats that have e if(extrasize) { // map file actually has extra header data that we want too preserve uchar *extrabuf = new uchar[extrasize]; - if(f->read(extrabuf, extrasize) != extrasize) { conoutf("\f3while reading map: header malformatted (3)"); delete f; return -7; } + if(f->read(extrabuf, extrasize) != extrasize) { conoutf("\f3while reading map: header malformatted (3)"); delete f; delete[] extrabuf; return -7; } unpackheaderextra(extrabuf, extrasize); delete[] extrabuf; }