foxygit / doom Log in
commit 3cf762118e2817f8e5ce59cc408bb8c876f5ba48
Author:     Simon Howard <fraggle@soulsphere.org>
AuthorDate: Sat Sep 2 19:25:10 2017 -0400
Commit:     Simon Howard <fraggle@soulsphere.org>
CommitDate: Sat Sep 2 19:25:10 2017 -0400

    Fix warnings about P_InitSwitchList() overruns.

    This function is weirdly written and the logic is hard to follow, so
    clean it up. This resolves warnings about array index overruns surfaced
    by cppcheck and blocking #939. Thanks @turol for discovering.
---
 src/doom/p_switch.c    | 69 +++++++++++++++++++++++---------------------------
 src/heretic/p_switch.c | 40 ++++++++++++++++-------------
 src/strife/p_switch.c  | 44 ++++++++++++++++----------------
 3 files changed, 75 insertions(+), 78 deletions(-)

diff --git a/src/doom/p_switch.c b/src/doom/p_switch.c
index ed4feeca..b9ef2195 100644
--- a/src/doom/p_switch.c
+++ b/src/doom/p_switch.c
@@ -61,7 +61,7 @@ switchlist_t alphSwitchList[] =
     {"SW1STON2",	"SW2STON2",	1},
     {"SW1STONE",	"SW2STONE",	1},
     {"SW1STRTN",	"SW2STRTN",	1},
-
+
     // Doom registered episodes 2&3 switches
     {"SW1BLUE",	"SW2BLUE",	2},
     {"SW1CMT",		"SW2CMT",	2},
@@ -73,7 +73,7 @@ switchlist_t alphSwitchList[] =
     {"SW1SKIN",	"SW2SKIN",	2},
     {"SW1VINE",	"SW2VINE",	2},
     {"SW1WOOD",	"SW2WOOD",	2},
-
+
     // Doom II switches
     {"SW1PANEL",	"SW2PANEL",	3},
     {"SW1ROCK",	"SW2ROCK",	3},
@@ -86,8 +86,6 @@ switchlist_t alphSwitchList[] =
     {"SW1TEK",		"SW2TEK",	3},
     {"SW1MARB",	"SW2MARB",	3},
     {"SW1SKULL",	"SW2SKULL",	3},
-
-    {"\0",		"\0",		0}
 };

 int		switchlist[MAXSWITCHES * 2];
@@ -100,45 +98,40 @@ button_t        buttonlist[MAXBUTTONS];
 //
 void P_InitSwitchList(void)
 {
-    int		i;
-    int		index;
-    int		episode;
-
-    episode = 1;
-
-    if (gamemode == registered || gamemode == retail)
-	episode = 2;
-    else
-	if ( gamemode == commercial )
-	    episode = 3;
-
-    for (index = 0,i = 0;i < MAXSWITCHES;i++)
+    int i, slindex, episode;
+
+    // Note that this is called "episode" here but it's actually something
+    // quite different. As we progress from Shareware->Registered->Doom II
+    // we support more switch textures.
+    switch (gamemode)
+    {
+        case registered:
+        case retail:
+            episode = 2;
+            break;
+        case commercial:
+            episode = 3;
+            break;
+        default:
+            episode = 1;
+            break;
+    }
+
+    slindex = 0;
+
+    for (i = 0; i < arrlen(alphSwitchList); i++)
     {
-	if (!alphSwitchList[i].episode)
-	{
-	    numswitches = index/2;
-	    switchlist[index] = -1;
-	    break;
-	}
-
 	if (alphSwitchList[i].episode <= episode)
 	{
-#if 0	// UNUSED - debug?
-	    int		value;
-
-	    if (R_CheckTextureNumForName(alphSwitchList[i].name1) < 0)
-	    {
-		I_Error("Can't find switch texture '%s'!",
-			alphSwitchList[i].name1);
-		continue;
-	    }
-
-	    value = R_TextureNumForName(alphSwitchList[i].name1);
-#endif
-	    switchlist[index++] = R_TextureNumForName(DEH_String(alphSwitchList[i].name1));
-	    switchlist[index++] = R_TextureNumForName(DEH_String(alphSwitchList[i].name2));
+	    switchlist[slindex++] =
+                R_TextureNumForName(DEH_String(alphSwitchList[i].name1));
+	    switchlist[slindex++] =
+                R_TextureNumForName(DEH_String(alphSwitchList[i].name2));
 	}
     }
+
+    numswitches = slindex / 2;
+    switchlist[slindex] = -1;
 }


diff --git a/src/heretic/p_switch.c b/src/heretic/p_switch.c
index 96d14074..c5422200 100644
--- a/src/heretic/p_switch.c
+++ b/src/heretic/p_switch.c
@@ -102,31 +102,35 @@ button_t buttonlist[MAXBUTTONS];

 void P_InitSwitchList(void)
 {
-    int i;
-    int index;
-    int episode;
+    int i, slindex, episode;

-    episode = 1;
-    if (gamemode != shareware)
+    // Note that this is called "episode" here but it's actually something
+    // quite different. As we progress from Shareware->Registered->Doom II
+    // we support more switch textures.
+    if (gamemode == shareware)
+    {
+        episode = 1;
+    }
+    else
+    {
         episode = 2;
+    }

-    for (index = 0, i = 0; i < MAXSWITCHES; i++)
-    {
-        if (!alphSwitchList[i].episode)
-        {
-            numswitches = index / 2;
-            switchlist[index] = -1;
-            break;
-        }
+    slindex = 0;

-        if (alphSwitchList[i].episode <= episode)
-        {
-            switchlist[index++] =
+    for (i = 0; i < arrlen(alphSwitchList); i++)
+    {
+	if (alphSwitchList[i].episode <= episode)
+	{
+	    switchlist[slindex++] =
                 R_TextureNumForName(DEH_String(alphSwitchList[i].name1));
-            switchlist[index++] =
+	    switchlist[slindex++] =
                 R_TextureNumForName(DEH_String(alphSwitchList[i].name2));
-        }
+	}
     }
+
+    numswitches = slindex / 2;
+    switchlist[slindex] = -1;
 }

 //==================================================================
diff --git a/src/strife/p_switch.c b/src/strife/p_switch.c
index a909a039..5eec0420 100644
--- a/src/strife/p_switch.c
+++ b/src/strife/p_switch.c
@@ -117,7 +117,6 @@ switchlist_t alphSwitchList[] =
     { "SWITE03",    "SWITE04",      2,  sfx_None    },
     { "SWTELP01",   "SWTELP02",     2,  sfx_None    },
     { "BRNSCN01",   "BRNSCN05",     2,  sfx_firxpl  },
-    { "\0",         "\0",           0,  sfx_None    }
 };

 int		switchlist[MAXSWITCHES * 2];
@@ -130,34 +129,35 @@ button_t        buttonlist[MAXBUTTONS];
 //
 void P_InitSwitchList(void)
 {
-    int		i;
-    int		index;
-    int		episode;
-
-    episode = 1;
+    int i, slindex, episode;

-    if(isregistered)
+    // Note that this is called "episode" here but it's actually something
+    // quite different. As we progress from Shareware->Registered->Doom II
+    // we support more switch textures.
+    if (isregistered)
+    {
         episode = 2;
-    // villsa [STRIFE] unused
-    /*else
-	if ( gamemode == commercial )
-	    episode = 3;*/
-
-    for(index = 0, i = 0; i < MAXSWITCHES; i++)
+    }
+    else
+    {
+        episode = 1;
+    }
+
+    slindex = 0;
+
+    for (i = 0; i < arrlen(alphSwitchList); i++)
     {
-	if(!alphSwitchList[i].episode)
-	{
-	    numswitches = index/2;
-	    switchlist[index] = -1;
-	    break;
-	}
-
 	if (alphSwitchList[i].episode <= episode)
 	{
-	    switchlist[index++] = R_TextureNumForName(DEH_String(alphSwitchList[i].name1));
-	    switchlist[index++] = R_TextureNumForName(DEH_String(alphSwitchList[i].name2));
+	    switchlist[slindex++] =
+                R_TextureNumForName(DEH_String(alphSwitchList[i].name1));
+	    switchlist[slindex++] =
+                R_TextureNumForName(DEH_String(alphSwitchList[i].name2));
 	}
     }
+
+    numswitches = slindex / 2;
+    switchlist[slindex] = -1;
 }