Guard all cases obj.getplayer() is used unchecked

obj.getplayer() can return -1, which can cause out-of-bounds indexing of
obj.entities, which is really bad. This was by far the most changes, as
obj.getplayer() is the most used entity-getting function that returns
-1, as well as the most-used function whose sentinel value goes
unchecked.

To deal with the usage of obj.getplayer() in mapclass::warpto(), I just
added general bounds checks inside that function instead of changing all
the callers.
This commit is contained in:
Misa
2020-06-12 20:36:08 -07:00
committed by Ethan Lee
parent 08e47e839f
commit beab344267
7 changed files with 784 additions and 408 deletions

View File

@@ -2371,8 +2371,9 @@ bool entityclass::updateentities( int i )
case 16: //MAVERICK BUS FOLLOWS HIS OWN RULES
if (entities[i].state == 0) //Init
{
int player = getplayer();
//first, y position
if (entities[getplayer()].yp > 14 * 8)
if (player > -1 && entities[player].yp > 14 * 8)
{
entities[i].tile = 120;
entities[i].yp = (28*8)-62;
@@ -2383,7 +2384,7 @@ bool entityclass::updateentities( int i )
entities[i].yp = 24;
}
//now, x position
if (entities[getplayer()].xp > 20 * 8)
if (player > -1 && entities[player].xp > 20 * 8)
{
//approach from the left
entities[i].xp = -64;
@@ -2592,7 +2593,11 @@ bool entityclass::updateentities( int i )
game.saverx = game.roomx;
game.savery = game.roomy;
game.savedir = entities[getplayer()].dir;
int player = getplayer();
if (player > -1)
{
game.savedir = entities[player].dir;
}
entities[i].state = 0;
}
break;
@@ -2622,11 +2627,11 @@ bool entityclass::updateentities( int i )
temp = getplayer();
if (game.gravitycontrol == 0)
{
if (entities[temp].vy < 3) entities[temp].vy = 3;
if (temp > -1 && entities[temp].vy < 3) entities[temp].vy = 3;
}
else
{
if (entities[temp].vy > -3) entities[temp].vy = -3;
if (temp > -1 && entities[temp].vy > -3) entities[temp].vy = -3;
}
}
else if (entities[i].state == 2)
@@ -2679,20 +2684,20 @@ bool entityclass::updateentities( int i )
if (entities[k].rule == 7) entities[k].tile = 6;
//Stay close to the hero!
int j = getplayer();
if (entities[j].xp > entities[i].xp + 5)
if (j > -1 && entities[j].xp > entities[i].xp + 5)
{
entities[i].dir = 1;
}
else if (entities[j].xp < entities[i].xp - 5)
else if (j > -1 && entities[j].xp < entities[i].xp - 5)
{
entities[i].dir = 0;
}
if (entities[j].xp > entities[i].xp + 45)
if (j > -1 && entities[j].xp > entities[i].xp + 45)
{
entities[i].ax = 3;
}
else if (entities[j].xp < entities[i].xp - 45)
else if (j > -1 && entities[j].xp < entities[i].xp - 45)
{
entities[i].ax = -3;
}
@@ -2710,20 +2715,20 @@ bool entityclass::updateentities( int i )
{
//Basic rules, don't change expression
int j = getplayer();
if (entities[j].xp > entities[i].xp + 5)
if (j > -1 && entities[j].xp > entities[i].xp + 5)
{
entities[i].dir = 1;
}
else if (entities[j].xp < entities[i].xp - 5)
else if (j > -1 && entities[j].xp < entities[i].xp - 5)
{
entities[i].dir = 0;
}
if (entities[j].xp > entities[i].xp + 45)
if (j > -1 && entities[j].xp > entities[i].xp + 45)
{
entities[i].ax = 3;
}
else if (entities[j].xp < entities[i].xp - 45)
else if (j > -1 && entities[j].xp < entities[i].xp - 45)
{
entities[i].ax = -3;
}
@@ -2733,20 +2738,20 @@ bool entityclass::updateentities( int i )
//Everything from 10 on is for cutscenes
//Basic rules, don't change expression
int j = getplayer();
if (entities[j].xp > entities[i].xp + 5)
if (j > -1 && entities[j].xp > entities[i].xp + 5)
{
entities[i].dir = 1;
}
else if (entities[j].xp < entities[i].xp - 5)
else if (j > -1 && entities[j].xp < entities[i].xp - 5)
{
entities[i].dir = 0;
}
if (entities[j].xp > entities[i].xp + 45)
if (j > -1 && entities[j].xp > entities[i].xp + 45)
{
entities[i].ax = 3;
}
else if (entities[j].xp < entities[i].xp - 45)
else if (j > -1 && entities[j].xp < entities[i].xp - 45)
{
entities[i].ax = -3;
}
@@ -2890,11 +2895,11 @@ bool entityclass::updateentities( int i )
{
//Stand still and face the player
int j = getplayer();
if (entities[j].xp > entities[i].xp + 5)
if (j > -1 && entities[j].xp > entities[i].xp + 5)
{
entities[i].dir = 1;
}
else if (entities[j].xp < entities[i].xp - 5)
else if (j > -1 && entities[j].xp < entities[i].xp - 5)
{
entities[i].dir = 0;
}
@@ -3006,7 +3011,7 @@ bool entityclass::updateentities( int i )
{
//follow player, but only if he's on the floor!
int j = getplayer();
if(entities[j].onground>0)
if(j > -1 && entities[j].onground>0)
{
if (entities[j].xp > entities[i].xp + 5)
{
@@ -3032,11 +3037,11 @@ bool entityclass::updateentities( int i )
}
else
{
if (entities[j].xp > entities[i].xp + 5)
if (j > -1 && entities[j].xp > entities[i].xp + 5)
{
entities[i].dir = 1;
}
else if (entities[j].xp < entities[i].xp - 5)
else if (j > -1 && entities[j].xp < entities[i].xp - 5)
{
entities[i].dir = 0;
}
@@ -3100,7 +3105,7 @@ bool entityclass::updateentities( int i )
case 51: //Vertical warp line
if (entities[i].state == 2){
int j=getplayer();
if(entities[j].xp<=307){
if(j > -1 && entities[j].xp<=307){
customwarpmodevon=false;
entities[i].state = 0;
}
@@ -3115,7 +3120,7 @@ bool entityclass::updateentities( int i )
case 52: //Vertical warp line
if (entities[i].state == 2){
int j=getplayer();
if(entities[j].xp<=307){
if(j > -1 && entities[j].xp<=307){
customwarpmodevon=false;
entities[i].state = 0;
}
@@ -3157,11 +3162,11 @@ bool entityclass::updateentities( int i )
{
//Basic rules, don't change expression
int j = getplayer();
if (entities[j].xp > entities[i].xp + 5)
if (j > -1 && entities[j].xp > entities[i].xp + 5)
{
entities[i].dir = 1;
}
else if (entities[j].xp < entities[i].xp - 5)
else if (j > -1 && entities[j].xp < entities[i].xp - 5)
{
entities[i].dir = 0;
}
@@ -3226,7 +3231,11 @@ bool entityclass::updateentities( int i )
game.saverx = game.roomx;
game.savery = game.roomy;
game.savedir = entities[getplayer()].dir;
int player = getplayer();
if (player > -1)
{
game.savedir = entities[player].dir;
}
entities[i].state = 0;
}
@@ -4464,7 +4473,7 @@ void entityclass::movingplatformfix( int t )
//If this intersects the player, then we move the player along it
int j = getplayer();
if (entitycollide(t, j))
if (j > -1 && entitycollide(t, j))
{
//ok, bollox, let's make sure
entities[j].yp = entities[j].yp + int(entities[j].vy);
@@ -4759,7 +4768,7 @@ void entityclass::entitycollisioncheck()
//can't have the player being stuck...
int j = getplayer();
skipdirblocks = true;
if (!testwallsx(j, entities[j].xp, entities[j].yp))
if (j > -1 && !testwallsx(j, entities[j].xp, entities[j].yp))
{
//Let's try to get out...
if (entities[j].rule == 0)