Skip to content

Commit 7b2423b

Browse files
committed
Fix accidental division by zero
1 parent 21ae5ba commit 7b2423b

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

libjsonexpr/src/functions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ bool check_overflow_sub(number_integer_t lhs, number_integer_t rhs) {
8989
}
9090

9191
bool check_overflow_mul(number_integer_t lhs, number_integer_t rhs) {
92-
return std::abs(lhs) > int_max / std::abs(rhs);
92+
return rhs != 0 && std::abs(lhs) > int_max / std::abs(rhs);
9393
}
9494

9595
#define MATHS_OPERATOR(NAME, OPERATOR) \

tests/src/number.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ TEST_CASE("muldiv", "[maths]") {
108108
}
109109

110110
SECTION("int") {
111+
CHECK(evaluate("2*1") == "2"_json);
112+
CHECK(evaluate("1*2") == "2"_json);
113+
CHECK(evaluate("1*0") == "0"_json);
114+
CHECK(evaluate("0*1") == "0"_json);
111115
CHECK(evaluate("2*4") == "8"_json);
112116
CHECK(evaluate("4/2") == "2"_json);
113117
CHECK(evaluate("2/4") == "0"_json);

0 commit comments

Comments
 (0)