Fix SIGFPE exception on INT_MIN/-1 and INT_MIN%-1 in C4Script

stable-5.3
Sven Eberhardt 2013-02-26 21:41:27 +01:00 committed by Armin Burgmeier
parent 4ff7d3c8c9
commit 59d8ef0bde
1 changed files with 6 additions and 0 deletions

View File

@ -318,6 +318,9 @@ C4Value C4AulExec::Exec(C4AulBCC *pCPos, bool fPassErrors)
C4Value *pPar1 = pCurVal - 1, *pPar2 = pCurVal;
if (!pPar2->_getInt())
throw new C4AulExecError("division by zero");
// INT_MIN/-1 cannot be represented in an int and would cause an uncaught exception
if (pPar1->_getInt()==0x80000000 && pPar2->_getInt()==-1)
throw new C4AulExecError("division overflow");
pPar1->SetInt(pPar1->_getInt() / pPar2->_getInt());
PopValue();
break;
@ -334,6 +337,9 @@ C4Value C4AulExec::Exec(C4AulBCC *pCPos, bool fPassErrors)
{
CheckOpPars(C4V_Int, C4V_Int, "%");
C4Value *pPar1 = pCurVal - 1, *pPar2 = pCurVal;
// INT_MIN%-1 cannot be represented in an int and would cause an uncaught exception
if (pPar1->_getInt()==0x80000000 && pPar2->_getInt()==-1)
throw new C4AulExecError("modulo division overflow");
if (pPar2->_getInt())
pPar1->SetInt(pPar1->_getInt() % pPar2->_getInt());
else