PCExhumed: Handle the rest of the possible overflows for ksqrt()

This commit is contained in:
sirlemonhead 2020-06-22 11:24:21 +01:00 committed by Christoph Oelckers
parent fc530a362d
commit b1c9ec1f47
6 changed files with 111 additions and 19 deletions

View file

@ -304,7 +304,18 @@ int CheckCloseRange(short nPlayer, int *x, int *y, int *z, short *nSector)
int ecx = sintable[150] >> 3; int ecx = sintable[150] >> 3;
if (ksqrt((hitX - *x) * (hitX - *x) + (hitY - *y) * (hitY - *y)) >= ecx) uint32_t xDiff = klabs(hitX - *x);
uint32_t yDiff = klabs(hitY - *y);
uint32_t sqrtNum = xDiff * xDiff + yDiff * yDiff;
if (sqrtNum > INT_MAX)
{
OSD_Printf("%s %d: overflow\n", EDUKE32_FUNCTION, __LINE__);
sqrtNum = INT_MAX;
}
if (ksqrt(sqrtNum) >= ecx)
return 0; return 0;
*x = hitX; *x = hitX;

View file

@ -828,10 +828,18 @@ void CreatePushBlock(int nSector)
for (i = 0; i < nWalls; i++) for (i = 0; i < nWalls; i++)
{ {
int x = xAvg - wall[startwall + i].x; uint32_t xDiff = klabs(xAvg - wall[startwall + i].x);
int y = yAvg - wall[startwall + i].y; uint32_t yDiff = klabs(yAvg - wall[startwall + i].y);
int nSqrt = ksqrt(x * x + y * y); uint32_t sqrtNum = xDiff * xDiff + yDiff * yDiff;
if (sqrtNum > INT_MAX)
{
OSD_Printf("%s %d: overflow\n", EDUKE32_FUNCTION, __LINE__);
sqrtNum = INT_MAX;
}
int nSqrt = ksqrt(sqrtNum);
if (nSqrt > var_28) { if (nSqrt > var_28) {
var_28 = nSqrt; var_28 = nSqrt;
} }
@ -1093,10 +1101,18 @@ void SetQuake(short nSprite, int nVal)
{ {
int nPlayerSprite = PlayerList[i].nSprite; int nPlayerSprite = PlayerList[i].nSprite;
int xDiff = sprite[nPlayerSprite].x - x; uint32_t xDiff = klabs((int32_t)((sprite[nPlayerSprite].x - x) >> 8));
int yDiff = sprite[nPlayerSprite].y - y; uint32_t yDiff = klabs((int32_t)((sprite[nPlayerSprite].y - y) >> 8));
int nSqrt = ksqrt((xDiff >> 8)* (xDiff >> 8) + (yDiff >> 8)* (yDiff >> 8)); uint32_t sqrtNum = xDiff * xDiff + yDiff * yDiff;
if (sqrtNum > INT_MAX)
{
OSD_Printf("%s %d: overflow\n", EDUKE32_FUNCTION, __LINE__);
sqrtNum = INT_MAX;
}
int nSqrt = ksqrt(sqrtNum);
int eax = nVal; int eax = nVal;
@ -1145,12 +1161,20 @@ int AngleChase(int nSprite, int nSprite2, int ebx, int ecx, int push1)
{ {
int nHeight = tilesiz[sprite[nSprite2].picnum].y * sprite[nSprite2].yrepeat * 2; int nHeight = tilesiz[sprite[nSprite2].picnum].y * sprite[nSprite2].yrepeat * 2;
int xDiff = sprite[nSprite2].x - sprite[nSprite].x; int nMyAngle = GetMyAngle(sprite[nSprite2].x - sprite[nSprite].x, sprite[nSprite2].y - sprite[nSprite].y);
int yDiff = sprite[nSprite2].y - sprite[nSprite].y;
int nMyAngle = GetMyAngle(xDiff, yDiff); uint32_t xDiff = klabs(sprite[nSprite2].x - sprite[nSprite].x);
uint32_t yDiff = klabs(sprite[nSprite2].y - sprite[nSprite].y);
int nSqrt = ksqrt(xDiff * xDiff + yDiff * yDiff); uint32_t sqrtNum = xDiff * xDiff + yDiff * yDiff;
if (sqrtNum > INT_MAX)
{
OSD_Printf("%s %d: overflow\n", EDUKE32_FUNCTION, __LINE__);
sqrtNum = INT_MAX;
}
int nSqrt = ksqrt(sqrtNum);
int var_18 = GetMyAngle(nSqrt, ((sprite[nSprite2].z - nHeight) - sprite[nSprite].z) >> 8); int var_18 = GetMyAngle(nSqrt, ((sprite[nSprite2].z - nHeight) - sprite[nSprite].z) >> 8);
@ -1194,7 +1218,15 @@ int AngleChase(int nSprite, int nSprite2, int ebx, int ecx, int push1)
int xshift = x >> 8; int xshift = x >> 8;
int yshift = y >> 8; int yshift = y >> 8;
int z = Sin(sprite[nSprite].zvel) * ksqrt((xshift * xshift) + (yshift * yshift)); uint32_t sqrtNum = xshift * xshift + yshift * yshift;
if (sqrtNum > INT_MAX)
{
OSD_Printf("%s %d: overflow\n", EDUKE32_FUNCTION, __LINE__);
sqrtNum = INT_MAX;
}
int z = Sin(sprite[nSprite].zvel) * ksqrt(sqrtNum);
return movesprite(nSprite, x >> 2, y >> 2, (z >> 13) + (Sin(ecx) >> 5), 0, 0, nClipType); return movesprite(nSprite, x >> 2, y >> 2, (z >> 13) + (Sin(ecx) >> 5), 0, 0, nClipType);
} }

View file

@ -1414,7 +1414,15 @@ loc_1AB8E:
playerX -= sprite[nPlayerSprite].x; playerX -= sprite[nPlayerSprite].x;
playerY -= sprite[nPlayerSprite].y; playerY -= sprite[nPlayerSprite].y;
totalvel[nPlayer] = ksqrt((playerY * playerY) + (playerX * playerX)); uint32_t sqrtNum = playerX * playerX + playerY * playerY;
if (sqrtNum > INT_MAX)
{
OSD_Printf("%s %d: overflow\n", EDUKE32_FUNCTION, __LINE__);
sqrtNum = INT_MAX;
}
totalvel[nPlayer] = ksqrt(sqrtNum);
int nViewSect = sprite[nPlayerSprite].sectnum; int nViewSect = sprite[nPlayerSprite].sectnum;

View file

@ -255,10 +255,18 @@ int QueenAngleChase(short nSprite, short nSprite2, int val1, int val2)
int edx = ((pSprite2->z - nTileY) - pSprite->z) >> 8; int edx = ((pSprite2->z - nTileY) - pSprite->z) >> 8;
int x = pSprite2->x - pSprite->x; uint32_t xDiff = klabs(pSprite2->x - pSprite->x);
int y = pSprite2->y - pSprite->y; uint32_t yDiff = klabs(pSprite2->y - pSprite->y);
int nSqrt = ksqrt(x * x + y * y); uint32_t sqrtVal = xDiff * xDiff + yDiff * yDiff;
if (sqrtVal > INT_MAX)
{
OSD_Printf("%s %d: overflow\n", EDUKE32_FUNCTION, __LINE__);
sqrtVal = INT_MAX;
}
int nSqrt = ksqrt(sqrtVal);
int var_14 = GetMyAngle(nSqrt, edx); int var_14 = GetMyAngle(nSqrt, edx);
@ -292,7 +300,18 @@ int QueenAngleChase(short nSprite, short nSprite2, int val1, int val2)
int v26 = x * ((val1 * Cos(nAngle)) >> 14); int v26 = x * ((val1 * Cos(nAngle)) >> 14);
int v27 = x * ((val1 * Sin(nAngle)) >> 14); int v27 = x * ((val1 * Sin(nAngle)) >> 14);
int nSqrt = ksqrt(((v26 >> 8) * (v26 >> 8)) + ((v27 >> 8) * (v27 >> 8))) * Sin(da); uint32_t xDiff = klabs((int32_t)(v26 >> 8));
uint32_t yDiff = klabs((int32_t)(v27 >> 8));
uint32_t sqrtNum = xDiff * xDiff + yDiff * yDiff;
if (sqrtNum > INT_MAX)
{
OSD_Printf("%s %d: overflow\n", EDUKE32_FUNCTION, __LINE__);
sqrtNum = INT_MAX;
}
int nSqrt = ksqrt(sqrtNum) * Sin(da);
return movesprite(nSprite, v26 >> 2, v27 >> 2, (Sin(bobangle) >> 5) + (nSqrt >> 13), 0, 0, CLIPMASK1); return movesprite(nSprite, v26 >> 2, v27 >> 2, (Sin(bobangle) >> 5) + (nSqrt >> 13), 0, 0, CLIPMASK1);
} }

View file

@ -1543,7 +1543,18 @@ int runlist_CheckRadialDamage(short nSprite)
int edi = 0; int edi = 0;
int nDist = ksqrt(x * x + y * y); uint32_t xDiff = klabs(x);
uint32_t yDiff = klabs(y);
uint32_t sqrtNum = xDiff * xDiff + yDiff * yDiff;
if (sqrtNum > INT_MAX)
{
OSD_Printf("%s %d: overflow\n", EDUKE32_FUNCTION, __LINE__);
sqrtNum = INT_MAX;
}
int nDist = ksqrt(sqrtNum);
if (nDist < nDamageRadius) if (nDist < nDamageRadius)
{ {

View file

@ -145,7 +145,18 @@ int BuildSnake(short nPlayer, short zVal)
hitsect = hitData.sect; hitsect = hitData.sect;
hitsprite = hitData.sprite; hitsprite = hitData.sprite;
int nSqrt = ksqrt(((hity - y) * (hity - y)) + ((hitx - x) * (hitx - x))); uint32_t xDiff = klabs(hitx - x);
uint32_t yDiff = klabs(hity - y);
uint32_t sqrtNum = xDiff * xDiff + yDiff * yDiff;
if (sqrtNum > INT_MAX)
{
OSD_Printf("%s %d: overflow\n", EDUKE32_FUNCTION, __LINE__);
sqrtNum = INT_MAX;
}
int nSqrt = ksqrt(sqrtNum);
if (nSqrt < (sintable[512] >> 4)) if (nSqrt < (sintable[512] >> 4))
{ {