1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00

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

This commit is contained in:
itchyny
2023-06-27 07:23:41 +09:00
committed by GitHub
parent 6864aa8630
commit fac5553b22
2 changed files with 8 additions and 2 deletions

View File

@ -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;

View File

@ -540,6 +540,10 @@ null
null
172
[(infinite, -infinite) % (1, -1)]
null
[0,0,0,0]
1 + tonumber + ("10" | tonumber)
4
15