Skip to content

Commit

Permalink
Fix overflow exception of the modulo operator (fix #1176)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jun 25, 2023
1 parent 6864aa8 commit 3e8ff6a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,11 @@ static jv f_divide(jq_state *jq, jv input, jv a, jv b) {
static jv f_mod(jq_state *jq, jv input, jv a, jv b) {
jv_free(input);
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
if ((intmax_t)jv_number_value(b) == 0)
intmax_t bi = (intmax_t)jv_number_value(b);
if (bi == 0)
return type_error2(a, b, "cannot be divided (remainder) because the divisor is zero");
jv r = jv_number((intmax_t)jv_number_value(a) % (intmax_t)jv_number_value(b));
// Check if the divisor is -1 to avoid overflow when the dividend is INTMAX_MIN.
jv r = jv_number(bi == -1 ? 0 : (intmax_t)jv_number_value(a) % bi);
jv_free(a);
jv_free(b);
return r;
Expand Down
4 changes: 4 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ null
null
172

[(infinite, -infinite) % (1, -1)]
null
[0,0,0,0]

1 + tonumber + ("10" | tonumber)
4
15
Expand Down

0 comments on commit 3e8ff6a

Please sign in to comment.