Merge pull request #1108 from dezgeg/fix-sigfpe

Fix SIGFPE from integer overflow during division
This commit is contained in:
Eelco Dolstra 2016-10-26 17:56:14 +02:00 committed by GitHub
commit e02a1352c1

View file

@ -1516,10 +1516,16 @@ static void prim_div(EvalState & state, const Pos & pos, Value * * args, Value &
NixFloat f2 = state.forceFloat(*args[1], pos); NixFloat f2 = state.forceFloat(*args[1], pos);
if (f2 == 0) throw EvalError(format("division by zero, at %1%") % pos); if (f2 == 0) throw EvalError(format("division by zero, at %1%") % pos);
if (args[0]->type == tFloat || args[1]->type == tFloat) if (args[0]->type == tFloat || args[1]->type == tFloat) {
mkFloat(v, state.forceFloat(*args[0], pos) / state.forceFloat(*args[1], pos)); mkFloat(v, state.forceFloat(*args[0], pos) / state.forceFloat(*args[1], pos));
else } else {
mkInt(v, state.forceInt(*args[0], pos) / state.forceInt(*args[1], pos)); NixInt i1 = state.forceInt(*args[0], pos);
NixInt i2 = state.forceInt(*args[1], pos);
/* Avoid division overflow as it might raise SIGFPE. */
if (i1 == std::numeric_limits<NixInt>::min() && i2 == -1)
throw EvalError(format("overflow in integer division, at %1%") % pos);
mkInt(v, i1 / i2);
}
} }