Use macros for special gamevar access range checking

git-svn-id: https://svn.eduke32.com/eduke32@6233 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2017-06-23 03:59:00 +00:00
parent 54a0d8d4ae
commit 49edb0e9f6

View file

@ -583,6 +583,15 @@ int __fastcall Gv_GetArrayValue(int const id, int index)
return returnValue; return returnValue;
} }
#define CHECK_INDEX(range) \
if (EDUKE32_PREDICT_FALSE((unsigned)arrayIndex >= range)) \
\
{ \
spriteNum = arrayIndex; \
goto badindex; \
\
}
int __fastcall Gv_GetVar(int gameVar, int spriteNum, int playerNum) int __fastcall Gv_GetVar(int gameVar, int spriteNum, int playerNum)
{ {
if (gameVar == g_thisActorVarID) if (gameVar == g_thisActorVarID)
@ -653,42 +662,23 @@ special:
case STRUCT_SPRITE: case STRUCT_SPRITE:
{ {
int const label = *insptr++; int const label = *insptr++;
arrayIndexVar = (EDUKE32_PREDICT_FALSE(ActorLabels[label].flags & LABEL_HASPARM2)) ? arrayIndexVar = (EDUKE32_PREDICT_FALSE(ActorLabels[label].flags & LABEL_HASPARM2)) ?
Gv_GetVar(*insptr++, spriteNum, playerNum) : 0; Gv_GetVar(*insptr++, spriteNum, playerNum) : 0;
CHECK_INDEX(MAXSPRITES);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXSPRITES))
{
spriteNum = arrayIndex;
goto badindex;
}
returnValue = VM_GetSprite(arrayIndex, label, arrayIndexVar); returnValue = VM_GetSprite(arrayIndex, label, arrayIndexVar);
break; break;
} }
case STRUCT_TSPR: case STRUCT_TSPR:
{ {
int const label = *insptr++; int const label = *insptr++;
CHECK_INDEX(MAXSPRITES);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXSPRITES))
{
spriteNum = arrayIndex;
goto badindex;
}
returnValue = VM_GetTsprite(arrayIndex, label); returnValue = VM_GetTsprite(arrayIndex, label);
break; break;
} }
case STRUCT_THISPROJECTILE: case STRUCT_THISPROJECTILE:
{ {
int const label = *insptr++; int const label = *insptr++;
CHECK_INDEX(MAXSPRITES);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXSPRITES))
{
spriteNum = arrayIndex;
goto badindex;
}
returnValue = VM_GetActiveProjectile(arrayIndex, label); returnValue = VM_GetActiveProjectile(arrayIndex, label);
break; break;
} }
@ -696,26 +686,14 @@ special:
case STRUCT_PROJECTILE: case STRUCT_PROJECTILE:
{ {
int const label = *insptr++; int const label = *insptr++;
CHECK_INDEX(MAXTILES);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXTILES))
{
spriteNum = arrayIndex;
goto badindex;
}
returnValue = VM_GetProjectile(arrayIndex, label); returnValue = VM_GetProjectile(arrayIndex, label);
break; break;
} }
case STRUCT_TILEDATA: case STRUCT_TILEDATA:
{ {
int const label = *insptr++; int const label = *insptr++;
CHECK_INDEX(MAXTILES);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXTILES))
{
spriteNum = arrayIndex;
goto badindex;
}
returnValue = VM_GetTileData(arrayIndex, label); returnValue = VM_GetTileData(arrayIndex, label);
break; break;
} }
@ -723,13 +701,7 @@ special:
case STRUCT_PALDATA: case STRUCT_PALDATA:
{ {
int const label = *insptr++; int const label = *insptr++;
CHECK_INDEX(MAXPALOOKUPS);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXPALOOKUPS))
{
spriteNum = arrayIndex;
goto badindex;
}
returnValue = VM_GetPalData(arrayIndex, label); returnValue = VM_GetPalData(arrayIndex, label);
break; break;
} }
@ -737,18 +709,10 @@ special:
case STRUCT_PLAYER: case STRUCT_PLAYER:
{ {
int const label = *insptr++; int const label = *insptr++;
if (arrayIndexVar == g_thisActorVarID) arrayIndex = vm.playerNum; if (arrayIndexVar == g_thisActorVarID) arrayIndex = vm.playerNum;
arrayIndexVar = (EDUKE32_PREDICT_FALSE(PlayerLabels[label].flags & LABEL_HASPARM2)) ? arrayIndexVar = (EDUKE32_PREDICT_FALSE(PlayerLabels[label].flags & LABEL_HASPARM2)) ?
Gv_GetVar(*insptr++, spriteNum, playerNum) : 0; Gv_GetVar(*insptr++, spriteNum, playerNum) : 0;
CHECK_INDEX(MAXPLAYERS);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXPLAYERS))
{
spriteNum = arrayIndex;
goto badindex;
}
returnValue = VM_GetPlayer(arrayIndex, label, arrayIndexVar); returnValue = VM_GetPlayer(arrayIndex, label, arrayIndexVar);
break; break;
} }
@ -757,13 +721,7 @@ special:
int const label = *insptr++; int const label = *insptr++;
if (arrayIndexVar == g_thisActorVarID) arrayIndex = vm.playerNum; if (arrayIndexVar == g_thisActorVarID) arrayIndex = vm.playerNum;
CHECK_INDEX(MAXPLAYERS);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXPLAYERS))
{
spriteNum = arrayIndex;
goto badindex;
}
returnValue = VM_GetPlayerInput(arrayIndex, label); returnValue = VM_GetPlayerInput(arrayIndex, label);
break; break;
} }
@ -775,22 +733,12 @@ special:
case STRUCT_SECTOR: case STRUCT_SECTOR:
if (arrayIndexVar == g_thisActorVarID) arrayIndex = sprite[vm.spriteNum].sectnum; if (arrayIndexVar == g_thisActorVarID) arrayIndex = sprite[vm.spriteNum].sectnum;
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXSECTORS)) CHECK_INDEX(MAXSECTORS);
{
spriteNum = arrayIndex;
insptr++;
goto badindex;
}
returnValue = VM_GetSector(arrayIndex, *insptr++); returnValue = VM_GetSector(arrayIndex, *insptr++);
break; break;
case STRUCT_WALL: case STRUCT_WALL:
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXWALLS)) CHECK_INDEX(MAXWALLS);
{
spriteNum = arrayIndex;
insptr++;
goto badindex;
}
returnValue = VM_GetWall(arrayIndex, *insptr++); returnValue = VM_GetWall(arrayIndex, *insptr++);
break; break;
@ -819,6 +767,8 @@ badindex:
return -1; return -1;
} }
#undef CHECK_INDEX
void __fastcall Gv_SetVar(int const gameVar, int const newValue, int const spriteNum, int const playerNum) void __fastcall Gv_SetVar(int const gameVar, int const newValue, int const spriteNum, int const playerNum)
{ {
int const varFlags = aGameVars[gameVar].flags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK); int const varFlags = aGameVars[gameVar].flags & (GAMEVAR_USER_MASK|GAMEVAR_PTR_MASK);
@ -882,6 +832,17 @@ static const char *gvxerrs[] = {
"Gv_GetVarX(): invalid pal ID", "Gv_GetVarX(): invalid pal ID",
}; };
#define CHECK_INDEX(arrayIndex, range, error) \
if (EDUKE32_PREDICT_FALSE((unsigned)arrayIndex >= range)) \
\
{ \
gameVar = arrayIndex; \
returnValue = error; \
goto badindex; \
\
}
int __fastcall Gv_GetSpecialVarX(int gameVar) int __fastcall Gv_GetSpecialVarX(int gameVar)
{ {
int returnValue = -1; int returnValue = -1;
@ -913,44 +874,22 @@ int __fastcall Gv_GetSpecialVarX(int gameVar)
case STRUCT_SPRITE: case STRUCT_SPRITE:
{ {
int const labelNum = *insptr++; int const labelNum = *insptr++;
arrayIndexVar = (EDUKE32_PREDICT_FALSE(ActorLabels[labelNum].flags & LABEL_HASPARM2)) ? Gv_GetVarX(*insptr++) : 0; arrayIndexVar = (EDUKE32_PREDICT_FALSE(ActorLabels[labelNum].flags & LABEL_HASPARM2)) ? Gv_GetVarX(*insptr++) : 0;
CHECK_INDEX(arrayIndex, MAXSPRITES, GVX_BADSPRITE);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXSPRITES))
{
gameVar = arrayIndex;
returnValue = GVX_BADSPRITE;
goto badindex;
}
returnValue = VM_GetSprite(arrayIndex, labelNum, arrayIndexVar); returnValue = VM_GetSprite(arrayIndex, labelNum, arrayIndexVar);
break; break;
} }
case STRUCT_TSPR: case STRUCT_TSPR:
{ {
int const labelNum = *insptr++; int const labelNum = *insptr++;
CHECK_INDEX(arrayIndex, MAXSPRITES, GVX_BADSPRITE);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXSPRITES))
{
gameVar = arrayIndex;
returnValue = GVX_BADSPRITE;
goto badindex;
}
returnValue = VM_GetTsprite(arrayIndex, labelNum); returnValue = VM_GetTsprite(arrayIndex, labelNum);
break; break;
} }
case STRUCT_THISPROJECTILE: case STRUCT_THISPROJECTILE:
{ {
int const labelNum = *insptr++; int const labelNum = *insptr++;
CHECK_INDEX(arrayIndex, MAXSPRITES, GVX_BADSPRITE);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXSPRITES))
{
gameVar = arrayIndex;
returnValue = GVX_BADSPRITE;
goto badindex;
}
returnValue = VM_GetActiveProjectile(arrayIndex, labelNum); returnValue = VM_GetActiveProjectile(arrayIndex, labelNum);
break; break;
} }
@ -958,28 +897,14 @@ int __fastcall Gv_GetSpecialVarX(int gameVar)
case STRUCT_PROJECTILE: case STRUCT_PROJECTILE:
{ {
int const labelNum = *insptr++; int const labelNum = *insptr++;
CHECK_INDEX(arrayIndex, MAXTILES, GVX_BADTILE);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXTILES))
{
gameVar = arrayIndex;
returnValue = GVX_BADTILE;
goto badindex;
}
returnValue = VM_GetProjectile(arrayIndex, labelNum); returnValue = VM_GetProjectile(arrayIndex, labelNum);
break; break;
} }
case STRUCT_TILEDATA: case STRUCT_TILEDATA:
{ {
int const labelNum = *insptr++; int const labelNum = *insptr++;
CHECK_INDEX(arrayIndex, MAXTILES, GVX_BADTILE);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXTILES))
{
gameVar = arrayIndex;
returnValue = GVX_BADTILE;
goto badindex;
}
returnValue = VM_GetTileData(arrayIndex, labelNum); returnValue = VM_GetTileData(arrayIndex, labelNum);
break; break;
} }
@ -987,14 +912,7 @@ int __fastcall Gv_GetSpecialVarX(int gameVar)
case STRUCT_PALDATA: case STRUCT_PALDATA:
{ {
int const labelNum = *insptr++; int const labelNum = *insptr++;
CHECK_INDEX(arrayIndex, MAXPALOOKUPS, GVX_BADPAL);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXPALOOKUPS))
{
gameVar = arrayIndex;
returnValue = GVX_BADPAL;
goto badindex;
}
returnValue = VM_GetPalData(arrayIndex, labelNum); returnValue = VM_GetPalData(arrayIndex, labelNum);
break; break;
} }
@ -1002,37 +920,20 @@ int __fastcall Gv_GetSpecialVarX(int gameVar)
case STRUCT_PLAYER: case STRUCT_PLAYER:
{ {
int const labelNum = *insptr++; int const labelNum = *insptr++;
if (arrayIndexVar == g_thisActorVarID) if (arrayIndexVar == g_thisActorVarID)
arrayIndex = vm.playerNum; arrayIndex = vm.playerNum;
arrayIndexVar = (EDUKE32_PREDICT_FALSE(PlayerLabels[labelNum].flags & LABEL_HASPARM2)) ? arrayIndexVar = (EDUKE32_PREDICT_FALSE(PlayerLabels[labelNum].flags & LABEL_HASPARM2)) ?
Gv_GetVarX(*insptr++) : 0; Gv_GetVarX(*insptr++) : 0;
CHECK_INDEX(arrayIndex, MAXPLAYERS, GVX_BADPLAYER);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXPLAYERS))
{
gameVar = arrayIndex;
returnValue = GVX_BADPLAYER;
goto badindex;
}
returnValue = VM_GetPlayer(arrayIndex, labelNum, arrayIndexVar); returnValue = VM_GetPlayer(arrayIndex, labelNum, arrayIndexVar);
break; break;
} }
case STRUCT_INPUT: case STRUCT_INPUT:
{ {
int const labelNum = *insptr++; int const labelNum = *insptr++;
if (arrayIndexVar == g_thisActorVarID) if (arrayIndexVar == g_thisActorVarID)
arrayIndex = vm.playerNum; arrayIndex = vm.playerNum;
CHECK_INDEX(arrayIndex, MAXPLAYERS, GVX_BADPLAYER);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXPLAYERS))
{
gameVar = arrayIndex;
returnValue = GVX_BADPLAYER;
goto badindex;
}
returnValue = VM_GetPlayerInput(arrayIndex, labelNum); returnValue = VM_GetPlayerInput(arrayIndex, labelNum);
break; break;
} }
@ -1045,25 +946,12 @@ int __fastcall Gv_GetSpecialVarX(int gameVar)
case STRUCT_SECTOR: case STRUCT_SECTOR:
if (arrayIndexVar == g_thisActorVarID) if (arrayIndexVar == g_thisActorVarID)
arrayIndex = sprite[vm.spriteNum].sectnum; arrayIndex = sprite[vm.spriteNum].sectnum;
CHECK_INDEX(arrayIndex, MAXSECTORS, GVX_BADSECTOR);
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXSECTORS))
{
gameVar = arrayIndex;
insptr++;
returnValue = GVX_BADSECTOR;
goto badindex;
}
returnValue = VM_GetSector(arrayIndex, *insptr++); returnValue = VM_GetSector(arrayIndex, *insptr++);
break; break;
case STRUCT_WALL: case STRUCT_WALL:
if (EDUKE32_PREDICT_FALSE((unsigned) arrayIndex >= MAXWALLS)) CHECK_INDEX(arrayIndex, MAXWALLS, GVX_BADWALL);
{
gameVar = arrayIndex;
insptr++;
returnValue = GVX_BADWALL;
goto badindex;
}
returnValue = VM_GetWall(arrayIndex, *insptr++); returnValue = VM_GetWall(arrayIndex, *insptr++);
break; break;
@ -1082,6 +970,8 @@ badindex:
return -1; return -1;
} }
#undef CHECK_INDEX
int __fastcall Gv_GetVarX(int gameVar) int __fastcall Gv_GetVarX(int gameVar)
{ {
if (gameVar == g_thisActorVarID) if (gameVar == g_thisActorVarID)