Skip to content
Myr

Fix overflow exception of the modulo operator (fix #1176) (#2629)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by itchyny · committed by GitHub · parent 6864aa8 · Browse files · 2 files+8 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+8 added−2 removed

  • tests:+4 added−0 removed
  • other:+4 added−2 removed

2 files+8 added−2 removed

modified:src/builtin.c+4 added-2 removed
394 unmodified lines
395396static jv f_mod(jq_state *jq, jv input, jv a, jv b) {397 jv_free(input);398 if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {399 if ((intmax_t)jv_number_value(b) == 0)399 intmax_t bi = (intmax_t)jv_number_value(b);400 if (bi == 0)401 return type_error2(a, b, "cannot be divided (remainder) because the divisor is zero");401 jv r = jv_number((intmax_t)jv_number_value(a) % (intmax_t)jv_number_value(b));402 // Check if the divisor is -1 to avoid overflow when the dividend is INTMAX_MIN.403 jv r = jv_number(bi == -1 ? 0 : (intmax_t)jv_number_value(a) % bi);404 jv_free(a);405 jv_free(b);406 return r;407 } else {
1451 unmodified lines
modified:tests/jq.test+4 added
538 unmodified lines
53949732 % 472540null541172542543[(infinite, -infinite) % (1, -1)]544null545[0,0,0,0]5465471 + tonumber + ("10" | tonumber)548454915550
1172 unmodified lines