From 322742d4b11b66670a5f2021ef647a7ec87fa4cd Mon Sep 17 00:00:00 2001 From: ZzZombo Date: Sat, 7 Feb 2015 23:35:23 +0800 Subject: [PATCH] - Fixed various instances of unused variables, accessing arrays out of bounds, initialization of non-primitive members in constructor's body, dead code, passing parameters by value instead of reference, usage of uninitialized variables, as reported by cppcheck. --- src/am_map.cpp | 13 ++++++++++--- src/b_bot.cpp | 4 ++-- src/b_func.cpp | 2 +- src/c_cmds.cpp | 3 +-- src/c_console.cpp | 3 +-- src/c_cvars.cpp | 27 +++++++-------------------- src/c_dispatch.cpp | 2 +- src/compatibility.cpp | 2 +- src/compatibility.h | 4 ++-- src/configfile.cpp | 5 ++--- src/d_net.cpp | 3 ++- src/d_protocol.cpp | 2 +- src/decallib.cpp | 11 +++-------- src/dobjtype.h | 2 +- src/dsectoreffect.cpp | 3 +-- src/files.cpp | 2 +- src/g_game.cpp | 6 ++++++ src/g_level.cpp | 2 -- src/g_level.h | 2 +- src/g_mapinfo.cpp | 2 -- src/i_net.cpp | 3 +-- src/info.cpp | 9 ++++----- src/info.h | 8 ++++---- src/m_argv.cpp | 7 ++----- src/m_png.cpp | 1 - src/menu/menu.h | 3 +-- src/tarray.h | 30 +++++++++++++++--------------- src/thingdef/thingdef_exp.h | 3 +-- 28 files changed, 72 insertions(+), 92 deletions(-) diff --git a/src/am_map.cpp b/src/am_map.cpp index 29cd60793..1529b4740 100644 --- a/src/am_map.cpp +++ b/src/am_map.cpp @@ -1234,9 +1234,16 @@ void AM_initVariables () for (pnum=0;pnumx >> FRACTOMAPBITS) - m_w/2; - m_y = (players[pnum].camera->y >> FRACTOMAPBITS) - m_h/2; + // [ZzZombo] no access out of bounds. + if(pnum>=MAXPLAYERS) + { + m_x=m_y=0; + } + else + { + m_x = (players[pnum].camera->x >> FRACTOMAPBITS) - m_w/2; + m_y = (players[pnum].camera->y >> FRACTOMAPBITS) - m_h/2; + } AM_changeWindowLoc(); // for saving & restoring diff --git a/src/b_bot.cpp b/src/b_bot.cpp index 05cac045b..548d10e2e 100644 --- a/src/b_bot.cpp +++ b/src/b_bot.cpp @@ -61,8 +61,8 @@ void DBot::Serialize (FArchive &arc) if (SaveVersion < 4515) { - angle_t savedyaw; - int savedpitch; + angle_t savedyaw=0; + int savedpitch=0; arc << savedyaw << savedpitch; } diff --git a/src/b_func.cpp b/src/b_func.cpp index 7165d2cc1..1c29c2cf6 100644 --- a/src/b_func.cpp +++ b/src/b_func.cpp @@ -513,7 +513,7 @@ angle_t DBot::FireRox (AActor *enemy, ticcmd_t *cmd) bglobal.SetBodyAt (enemy->x + FixedMul(enemy->velx, (m+2*FRACUNIT)), enemy->y + FixedMul(enemy->vely, (m+2*FRACUNIT)), ONFLOORZ, 1); - dist = P_AproxDistance(actor->x-bglobal.body1->x, actor->y-bglobal.body1->y); + //try the predicted location if (P_CheckSight (actor, bglobal.body1, SF_IGNOREVISIBILITY)) //See the predicted location, so give a test missile { diff --git a/src/c_cmds.cpp b/src/c_cmds.cpp index 9290c36c3..9c30c8f4f 100644 --- a/src/c_cmds.cpp +++ b/src/c_cmds.cpp @@ -1028,8 +1028,7 @@ CCMD(nextsecret) TEXTCOLOR_NORMAL " is for single-player only.\n"); return; } - char *next = NULL; - + if (level.NextSecretMap.Len() > 0 && level.NextSecretMap.Compare("enDSeQ", 6)) { G_DeferedInitNew(level.NextSecretMap); diff --git a/src/c_console.cpp b/src/c_console.cpp index e3e3d98fe..3be72d953 100644 --- a/src/c_console.cpp +++ b/src/c_console.cpp @@ -856,11 +856,10 @@ void C_DrawConsole (bool hw2d) } else if (ConBottom) { - int visheight, realheight; + int visheight; FTexture *conpic = TexMan[conback]; visheight = ConBottom; - realheight = (visheight * conpic->GetHeight()) / SCREENHEIGHT; screen->DrawTexture (conpic, 0, visheight - screen->GetHeight(), DTA_DestWidth, screen->GetWidth(), diff --git a/src/c_cvars.cpp b/src/c_cvars.cpp index c770bcbcf..c7a71197b 100644 --- a/src/c_cvars.cpp +++ b/src/c_cvars.cpp @@ -439,7 +439,7 @@ static BYTE HexToByte (const char *hex) UCVarValue FBaseCVar::FromString (const char *value, ECVarType type) { UCVarValue ret; - int i; + int i=0; switch (type) { @@ -475,39 +475,29 @@ UCVarValue FBaseCVar::FromString (const char *value, ECVarType type) // 0 1 2 3 ret.pGUID = NULL; - for (i = 0; i < 38; ++i) + if(value) + for (; i < 38; i++) { - if (value[i] == 0) - { - break; - } - bool goodv = true; switch (i) { case 0: if (value[i] != '{') - goodv = false; - break; + break; case 9: case 14: case 19: case 24: if (value[i] != '-') - goodv = false; - break; + break; case 37: if (value[i] != '}') - goodv = false; - break; + break; default: if (value[i] < '0' || (value[i] > '9' && value[i] < 'A') || (value[i] > 'F' && value[i] < 'a') || value[i] > 'f') - { - goodv = false; - } - break; + break; } } if (i == 38 && value[i] == 0) @@ -1673,9 +1663,6 @@ void FBaseCVar::ListVars (const char *filter, bool plain) if (CheckWildcards (filter, var->GetName())) { DWORD flags = var->GetFlags(); - UCVarValue val; - - val = var->GetGenericRep (CVAR_String); if (plain) { // plain formatting does not include user-defined cvars if (!(flags & CVAR_UNSETTABLE)) diff --git a/src/c_dispatch.cpp b/src/c_dispatch.cpp index 7a6748fdc..8755e81e1 100644 --- a/src/c_dispatch.cpp +++ b/src/c_dispatch.cpp @@ -1336,7 +1336,7 @@ CCMD (alias) } else { - alias = new FConsoleAlias (argv[1], argv[2], ParsingKeyConf); + new FConsoleAlias (argv[1], argv[2], ParsingKeyConf); } } } diff --git a/src/compatibility.cpp b/src/compatibility.cpp index 8a4341b76..4ddc670e9 100644 --- a/src/compatibility.cpp +++ b/src/compatibility.cpp @@ -448,7 +448,7 @@ void SetCompatibilityParams() { unsigned i = ii_compatparams; - while (CompatParams[i] != CP_END && i < CompatParams.Size()) + while (i < CompatParams.Size() && CompatParams[i] != CP_END) { switch (CompatParams[i]) { diff --git a/src/compatibility.h b/src/compatibility.h index cf4dce2f7..d37d25903 100644 --- a/src/compatibility.h +++ b/src/compatibility.h @@ -20,11 +20,11 @@ struct FCompatValues struct FMD5HashTraits { - hash_t Hash(const FMD5Holder key) + hash_t Hash(const FMD5Holder &key) { return key.Hash; } - int Compare(const FMD5Holder left, const FMD5Holder right) + int Compare(const FMD5Holder &left, const FMD5Holder &right) { return left.DWords[0] != right.DWords[0] || left.DWords[1] != right.DWords[1] || diff --git a/src/configfile.cpp b/src/configfile.cpp index b8e37d91c..4d642a81d 100644 --- a/src/configfile.cpp +++ b/src/configfile.cpp @@ -50,12 +50,11 @@ static FRandom pr_endtag; // //==================================================================== -FConfigFile::FConfigFile () +FConfigFile::FConfigFile () : PathName(NAME_None) { Sections = CurrentSection = NULL; LastSectionPtr = &Sections; CurrentEntry = NULL; - PathName = ""; OkayToWrite = true; FileExisted = true; } @@ -836,7 +835,7 @@ const char *FConfigFile::GenerateEndTag(const char *value) for (int i = 0; i < 5; ++i) { - DWORD three_bytes = (rand_bytes[i*3] << 16) | (rand_bytes[i*3+1] << 8) | (rand_bytes[i*3+2]); + //DWORD three_bytes = (rand_bytes[i*3] << 16) | (rand_bytes[i*3+1] << 8) | (rand_bytes[i*3+2]); // ??? EndTag[4+i*4 ] = Base64Table[rand_bytes[i*3] >> 2]; EndTag[4+i*4+1] = Base64Table[((rand_bytes[i*3] & 3) << 4) | (rand_bytes[i*3+1] >> 4)]; EndTag[4+i*4+2] = Base64Table[((rand_bytes[i*3+1] & 15) << 2) | (rand_bytes[i*3+2] >> 6)]; diff --git a/src/d_net.cpp b/src/d_net.cpp index b3e70d410..aa4c4ee1b 100644 --- a/src/d_net.cpp +++ b/src/d_net.cpp @@ -2054,7 +2054,8 @@ void FDynamicBuffer::SetData (const BYTE *data, int len) } else { - len = 0; + m_Len = 0; + M_Free((void *)data); } } diff --git a/src/d_protocol.cpp b/src/d_protocol.cpp index 11e682cda..3613f84db 100644 --- a/src/d_protocol.cpp +++ b/src/d_protocol.cpp @@ -500,5 +500,5 @@ void SkipChunk (BYTE **stream) int len; len = ReadLong (stream); - stream += len + (len & 1); + *stream += len + (len & 1); } diff --git a/src/decallib.cpp b/src/decallib.cpp index 91827409b..79c961099 100644 --- a/src/decallib.cpp +++ b/src/decallib.cpp @@ -443,7 +443,6 @@ void FDecalLib::ParseDecal (FScanner &sc) FString decalName; WORD decalNum; FDecalTemplate newdecal; - int code; FTextureID picnum; int lumpnum; @@ -467,7 +466,7 @@ void FDecalLib::ParseDecal (FScanner &sc) AddDecal (decalName, decalNum, newdecal); break; } - switch ((code = sc.MustMatchString (DecalKeywords))) + switch (sc.MustMatchString (DecalKeywords)) { case DECAL_XSCALE: newdecal.ScaleX = ReadScale (sc); @@ -763,8 +762,6 @@ void FDecalLib::ParseSlider (FScanner &sc) } else if (sc.Compare ("DistX")) { - sc.MustGetFloat (); - distX = (fixed_t)(sc.Float * FRACUNIT); Printf ("DistX in slider decal %s is unsupported\n", sliderName.GetChars()); } else if (sc.Compare ("DistY")) @@ -1024,9 +1021,8 @@ FDecalLib::FTranslation *FDecalLib::GenerateTranslation (DWORD start, DWORD end) return trans; } -FDecalBase::FDecalBase () +FDecalBase::FDecalBase () : Name(NAME_None) { - Name = NAME_None; } FDecalBase::~FDecalBase () @@ -1139,9 +1135,8 @@ const FDecalTemplate *FDecalGroup::GetDecal () const return static_cast(remember); } -FDecalAnimator::FDecalAnimator (const char *name) +FDecalAnimator::FDecalAnimator (const char *name) : Name(name) { - Name = name; } FDecalAnimator::~FDecalAnimator () diff --git a/src/dobjtype.h b/src/dobjtype.h index 256e52ad8..252287e3e 100644 --- a/src/dobjtype.h +++ b/src/dobjtype.h @@ -24,7 +24,7 @@ struct PSymbol FName SymbolName; protected: - PSymbol(FName name, ESymbolType type) { SymbolType = type; SymbolName = name; } + PSymbol(FName name, ESymbolType type):SymbolName(name) { SymbolType = type; } }; // A constant value --------------------------------------------------------- diff --git a/src/dsectoreffect.cpp b/src/dsectoreffect.cpp index 6732a19dc..82a7deb6f 100644 --- a/src/dsectoreffect.cpp +++ b/src/dsectoreffect.cpp @@ -78,9 +78,8 @@ DMover::DMover () } DMover::DMover (sector_t *sector) - : DSectorEffect (sector) + : DSectorEffect (sector), interpolation(NULL) { - interpolation = NULL; } void DMover::Destroy() diff --git a/src/files.cpp b/src/files.cpp index d7dad642e..52aa891c9 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -53,7 +53,7 @@ //========================================================================== FileReader::FileReader () -: File(NULL), Length(0), StartPos(0), CloseOnDestruct(false) +: File(NULL), Length(0), StartPos(0), FilePos(0), CloseOnDestruct(false) { } diff --git a/src/g_game.cpp b/src/g_game.cpp index ba53254c2..e5df703ea 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -2589,6 +2589,12 @@ bool G_ProcessIFFDemo (FString &mapname) demo_p = nextchunk; } + if (!headerHit) + { + Printf ("Demo has no header!\n"); + return true; + } + if (!numPlayers) { Printf ("Demo has no players!\n"); diff --git a/src/g_level.cpp b/src/g_level.cpp index 713847b05..b6cc1cd84 100644 --- a/src/g_level.cpp +++ b/src/g_level.cpp @@ -367,7 +367,6 @@ static void InitPlayerClasses () void G_InitNew (const char *mapname, bool bTitleLevel) { - EGameSpeed oldSpeed; bool wantFast; int i; @@ -454,7 +453,6 @@ void G_InitNew (const char *mapname, bool bTitleLevel) I_Error ("Could not find map %s\n", mapname); } - oldSpeed = GameSpeed; wantFast = !!G_SkillProperty(SKILLP_FastMonsters); GameSpeed = wantFast ? SPEED_Fast : SPEED_Normal; diff --git a/src/g_level.h b/src/g_level.h index 6e07b0c74..aaddfce3c 100644 --- a/src/g_level.h +++ b/src/g_level.h @@ -241,7 +241,7 @@ struct FOptionalMapinfoData { FOptionalMapinfoData *Next; FName identifier; - FOptionalMapinfoData() { Next = NULL; identifier = NAME_None; } + FOptionalMapinfoData():identifier(NAME_None) { Next = NULL; } virtual ~FOptionalMapinfoData() {} virtual FOptionalMapinfoData *Clone() const = 0; }; diff --git a/src/g_mapinfo.cpp b/src/g_mapinfo.cpp index c44f81ab2..6d49b9c17 100644 --- a/src/g_mapinfo.cpp +++ b/src/g_mapinfo.cpp @@ -705,8 +705,6 @@ void FMapInfoParser::ParseCluster() } else if (sc.Compare("music")) { - int order = 0; - ParseAssign(); ParseMusic(clusterinfo->MessageMusic, clusterinfo->musicorder); } diff --git a/src/i_net.cpp b/src/i_net.cpp index 188d2871e..da1063bc9 100644 --- a/src/i_net.cpp +++ b/src/i_net.cpp @@ -238,7 +238,7 @@ void PacketSend (void) else { // Printf("send %d\n", doomcom.datalength); - c = sendto(mysocket, (char *)doomcom.data, doomcom.datalength, + /*c = */sendto(mysocket, (char *)doomcom.data, doomcom.datalength, 0, (sockaddr *)&sendaddress[doomcom.remotenode], sizeof(sendaddress[doomcom.remotenode])); } @@ -800,7 +800,6 @@ bool Guest_WaitForOthers (void *userdata) { int node; - packet.NumNodes = packet.NumNodes; doomcom.numnodes = packet.NumNodes + 2; sendplayer[0] = packet.ConsoleNum; // My player number doomcom.consoleplayer = packet.ConsoleNum; diff --git a/src/info.cpp b/src/info.cpp index 0a8d92217..1da057fe2 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -158,7 +158,6 @@ void FActorInfo::StaticSetActorNums () void FActorInfo::RegisterIDs () { const PClass *cls = PClass::FindClass(Class->TypeName); - bool set = false; if (GameFilter == GAME_Any || (GameFilter & gameinfo.gametype)) { @@ -578,17 +577,17 @@ CCMD (summonfoe) TMap GlobalDamageDefinitions; -void DamageTypeDefinition::Apply(FName const type) +void DamageTypeDefinition::Apply(FName const &type) { GlobalDamageDefinitions[type] = *this; } -DamageTypeDefinition *DamageTypeDefinition::Get(FName const type) +DamageTypeDefinition *DamageTypeDefinition::Get(FName const &type) { return GlobalDamageDefinitions.CheckKey(type); } -bool DamageTypeDefinition::IgnoreArmor(FName const type) +bool DamageTypeDefinition::IgnoreArmor(FName const &type) { DamageTypeDefinition *dtd = Get(type); if (dtd) return dtd->NoArmor; @@ -610,7 +609,7 @@ bool DamageTypeDefinition::IgnoreArmor(FName const type) // //========================================================================== -int DamageTypeDefinition::ApplyMobjDamageFactor(int damage, FName const type, DmgFactors const * const factors) +int DamageTypeDefinition::ApplyMobjDamageFactor(int damage, FName const &type, DmgFactors const * const factors) { if (factors) { diff --git a/src/info.h b/src/info.h index 51c4bd629..0a6326c9a 100644 --- a/src/info.h +++ b/src/info.h @@ -217,7 +217,7 @@ public: bool ReplaceFactor; bool NoArmor; - void Apply(FName const type); + void Apply(FName const &type); void Clear() { DefaultFactor = FRACUNIT; @@ -225,9 +225,9 @@ public: NoArmor = false; } - static DamageTypeDefinition *Get(FName const type); - static bool IgnoreArmor(FName const type); - static int ApplyMobjDamageFactor(int damage, FName const type, DmgFactors const * const factors); + static DamageTypeDefinition *Get(FName const &type); + static bool IgnoreArmor(FName const &type); + static int ApplyMobjDamageFactor(int damage, FName const &type, DmgFactors const * const factors); }; diff --git a/src/m_argv.cpp b/src/m_argv.cpp index 816a2d54c..ef3f59fef 100644 --- a/src/m_argv.cpp +++ b/src/m_argv.cpp @@ -55,10 +55,9 @@ DArgs::DArgs() // //=========================================================================== -DArgs::DArgs(const DArgs &other) -: DObject() +DArgs::DArgs(const DArgs &other):Argv(other.Argv), + DObject() { - Argv = other.Argv; } //=========================================================================== @@ -263,7 +262,6 @@ void DArgs::RemoveArgs(const char *check) const char *DArgs::GetArg(int arg) const { return ((unsigned)arg < Argv.Size()) ? Argv[arg].GetChars() : NULL; - return Argv[arg]; } //=========================================================================== @@ -351,7 +349,6 @@ void DArgs::RemoveArg(int argindex) void DArgs::CollectFiles(const char *param, const char *extension) { TArray work; - DArgs *out = new DArgs; unsigned int i; size_t extlen = extension == NULL ? 0 : strlen(extension); diff --git a/src/m_png.cpp b/src/m_png.cpp index 2bec48103..0c8b6ab18 100644 --- a/src/m_png.cpp +++ b/src/m_png.cpp @@ -1023,7 +1023,6 @@ bool M_SaveBitmap(const BYTE *from, ESSType color_type, int width, int height, i } } - y = sizeof(buffer) - stream.avail_out; deflateEnd (&stream); if (err != Z_STREAM_END) diff --git a/src/menu/menu.h b/src/menu/menu.h index 3712c9065..b256ccb2f 100644 --- a/src/menu/menu.h +++ b/src/menu/menu.h @@ -261,11 +261,10 @@ protected: public: bool mEnabled; - FListMenuItem(int xpos = 0, int ypos = 0, FName action = NAME_None) + FListMenuItem(int xpos = 0, int ypos = 0, FName action = NAME_None):mAction(action) { mXpos = xpos; mYpos = ypos; - mAction = action; mEnabled = true; } diff --git a/src/tarray.h b/src/tarray.h index d32a688df..790692e45 100644 --- a/src/tarray.h +++ b/src/tarray.h @@ -416,10 +416,10 @@ typedef unsigned int hash_t; template struct THashTraits { // Returns the hash value for a key. - hash_t Hash(const KT key) { return (hash_t)(intptr_t)key; } + hash_t Hash(const KT &key) { return (hash_t)(intptr_t)key; } // Compares two keys, returning zero if they are the same. - int Compare(const KT left, const KT right) { return left != right; } + int Compare(const KT &left, const KT &right) { return left != right; } }; template struct TValueTraits @@ -547,12 +547,12 @@ public: // //======================================================================= - VT &operator[] (const KT key) + VT &operator[] (const KT &key) { return GetNode(key)->Pair.Value; } - const VT &operator[] (const KT key) const + const VT &operator[] (const KT &key) const { return GetNode(key)->Pair.Value; } @@ -566,13 +566,13 @@ public: // //======================================================================= - VT *CheckKey (const KT key) + VT *CheckKey (const KT &key) { Node *n = FindKey(key); return n != NULL ? &n->Pair.Value : NULL; } - const VT *CheckKey (const KT key) const + const VT *CheckKey (const KT &key) const { const Node *n = FindKey(key); return n != NULL ? &n->Pair.Value : NULL; @@ -591,7 +591,7 @@ public: // //======================================================================= - VT &Insert(const KT key, const VT &value) + VT &Insert(const KT &key, const VT &value) { Node *n = FindKey(key); if (n != NULL) @@ -614,7 +614,7 @@ public: // //======================================================================= - void Remove(const KT key) + void Remove(const KT &key) { DelKey(key); } @@ -649,13 +649,13 @@ protected: hash_t Size; /* must be a power of 2 */ hash_t NumUsed; - const Node *MainPosition(const KT k) const + const Node *MainPosition(const KT &k) const { HashTraits Traits; return &Nodes[Traits.Hash(k) & (Size - 1)]; } - Node *MainPosition(const KT k) + Node *MainPosition(const KT &k) { HashTraits Traits; return &Nodes[Traits.Hash(k) & (Size - 1)]; @@ -736,7 +736,7 @@ protected: ** ** The Value field is left unconstructed. */ - Node *NewKey(const KT key) + Node *NewKey(const KT &key) { Node *mp = MainPosition(key); if (!mp->IsNil()) @@ -775,7 +775,7 @@ protected: return mp; } - void DelKey(const KT key) + void DelKey(const KT &key) { Node *mp = MainPosition(key), **mpp; HashTraits Traits; @@ -814,7 +814,7 @@ protected: } } - Node *FindKey(const KT key) + Node *FindKey(const KT &key) { HashTraits Traits; Node *n = MainPosition(key); @@ -825,7 +825,7 @@ protected: return n == NULL || n->IsNil() ? NULL : n; } - const Node *FindKey(const KT key) const + const Node *FindKey(const KT &key) const { HashTraits Traits; const Node *n = MainPosition(key); @@ -836,7 +836,7 @@ protected: return n == NULL || n->IsNil() ? NULL : n; } - Node *GetNode(const KT key) + Node *GetNode(const KT &key) { Node *n = FindKey(key); if (n != NULL) diff --git a/src/thingdef/thingdef_exp.h b/src/thingdef/thingdef_exp.h index 807ffcd87..1c46ab6b2 100644 --- a/src/thingdef/thingdef_exp.h +++ b/src/thingdef/thingdef_exp.h @@ -152,10 +152,9 @@ struct ExpVal class FxExpression { protected: - FxExpression(const FScriptPosition &pos) + FxExpression(const FScriptPosition &pos):ScriptPosition(pos) { isresolved = false; - ScriptPosition = pos; } public: virtual ~FxExpression() {}