From b1c9ec1f47bb033e7315c5b5371eef573e41f027 Mon Sep 17 00:00:00 2001 From: sirlemonhead Date: Mon, 22 Jun 2020 11:24:21 +0100 Subject: [PATCH] PCExhumed: Handle the rest of the possible overflows for ksqrt() --- source/exhumed/src/gun.cpp | 13 +++++++- source/exhumed/src/move.cpp | 54 +++++++++++++++++++++++++++------- source/exhumed/src/player.cpp | 10 ++++++- source/exhumed/src/queen.cpp | 27 ++++++++++++++--- source/exhumed/src/runlist.cpp | 13 +++++++- source/exhumed/src/snake.cpp | 13 +++++++- 6 files changed, 111 insertions(+), 19 deletions(-) diff --git a/source/exhumed/src/gun.cpp b/source/exhumed/src/gun.cpp index 31b54de16..e52513601 100644 --- a/source/exhumed/src/gun.cpp +++ b/source/exhumed/src/gun.cpp @@ -304,7 +304,18 @@ int CheckCloseRange(short nPlayer, int *x, int *y, int *z, short *nSector) 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; *x = hitX; diff --git a/source/exhumed/src/move.cpp b/source/exhumed/src/move.cpp index 0c95b182d..45bb94c83 100644 --- a/source/exhumed/src/move.cpp +++ b/source/exhumed/src/move.cpp @@ -828,10 +828,18 @@ void CreatePushBlock(int nSector) for (i = 0; i < nWalls; i++) { - int x = xAvg - wall[startwall + i].x; - int y = yAvg - wall[startwall + i].y; + uint32_t xDiff = klabs(xAvg - wall[startwall + i].x); + 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) { var_28 = nSqrt; } @@ -1093,10 +1101,18 @@ void SetQuake(short nSprite, int nVal) { int nPlayerSprite = PlayerList[i].nSprite; - int xDiff = sprite[nPlayerSprite].x - x; - int yDiff = sprite[nPlayerSprite].y - y; + uint32_t xDiff = klabs((int32_t)((sprite[nPlayerSprite].x - x) >> 8)); + 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; @@ -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 xDiff = sprite[nSprite2].x - sprite[nSprite].x; - int yDiff = sprite[nSprite2].y - sprite[nSprite].y; + int nMyAngle = GetMyAngle(sprite[nSprite2].x - sprite[nSprite].x, 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); @@ -1194,7 +1218,15 @@ int AngleChase(int nSprite, int nSprite2, int ebx, int ecx, int push1) int xshift = x >> 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); } diff --git a/source/exhumed/src/player.cpp b/source/exhumed/src/player.cpp index dd9897b86..2e03400e3 100644 --- a/source/exhumed/src/player.cpp +++ b/source/exhumed/src/player.cpp @@ -1414,7 +1414,15 @@ loc_1AB8E: playerX -= sprite[nPlayerSprite].x; 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; diff --git a/source/exhumed/src/queen.cpp b/source/exhumed/src/queen.cpp index 7e145e21c..d6dfb4f69 100644 --- a/source/exhumed/src/queen.cpp +++ b/source/exhumed/src/queen.cpp @@ -255,10 +255,18 @@ int QueenAngleChase(short nSprite, short nSprite2, int val1, int val2) int edx = ((pSprite2->z - nTileY) - pSprite->z) >> 8; - int x = pSprite2->x - pSprite->x; - int y = pSprite2->y - pSprite->y; + uint32_t xDiff = klabs(pSprite2->x - pSprite->x); + 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); @@ -292,7 +300,18 @@ int QueenAngleChase(short nSprite, short nSprite2, int val1, int val2) int v26 = x * ((val1 * Cos(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); } diff --git a/source/exhumed/src/runlist.cpp b/source/exhumed/src/runlist.cpp index f11499ddd..c6d22801f 100644 --- a/source/exhumed/src/runlist.cpp +++ b/source/exhumed/src/runlist.cpp @@ -1543,7 +1543,18 @@ int runlist_CheckRadialDamage(short nSprite) 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) { diff --git a/source/exhumed/src/snake.cpp b/source/exhumed/src/snake.cpp index ab6285a01..fc9e7c3be 100644 --- a/source/exhumed/src/snake.cpp +++ b/source/exhumed/src/snake.cpp @@ -145,7 +145,18 @@ int BuildSnake(short nPlayer, short zVal) hitsect = hitData.sect; 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)) {