tinyexpr: Prevent possible division by zero

This is possibly not actually undefined behavior because IEEE754
defines _more_ of division-by-zero, but let's be careful.

Fixes #2852.
This commit is contained in:
Fabian Homborg 2019-02-04 16:29:33 +01:00
parent 4cc168ae11
commit 9f469e576b

View File

@ -202,7 +202,13 @@ static const te_builtin *find_builtin(const char *name, int len) {
static constexpr double add(double a, double b) {return a + b;}
static constexpr double sub(double a, double b) {return a - b;}
static constexpr double mul(double a, double b) {return a * b;}
static constexpr double divide(double a, double b) {return a / b;}
static constexpr double divide(double a, double b) {
// If b isn't zero, divide.
// If a isn't zero, return signed INFINITY.
// Else, return NAN.
return b ? a / b : a ? copysign(1, a) * copysign(1,b) * INFINITY : NAN;
}
static constexpr double negate(double a) {return -a;}
void next_token(state *s) {