Fix constant folding of division and reminder with zero divisor
Previously constant folding of zero division (e.x. 1/0) produces a compile error. This was incorrectly implemented by checking if the division result is infinite, so produces wrong results compared to the query where no constant folding is processed (e.x. 1e308/0.1). This patch delays the operation when the divisor is zero. This makes the results more consistent, but changes the exit code on zero division from 3 to 5. Also 0/0 now produces the zero division error, not NaN. This patch also fixes the modulo operation. Previously constant folding logic does not take care of the % operator, but now it folds if the both dividend and divisor are safe numbers to cast to the integer type, and the divisor is not zero. This patch also fixes some code that relies on undefined cast behaviors in C. The modulo operation produces NaN if either the dividend or divisor is NaN.
This commit is contained in:
1 parent
6716e23ae6
commit
0f80921268
7 files changed
+488
-449
No files matched your search
+12
-3
@@ -383,7 +383,7 @@ static jv f_multiply(jq_state *jq, jv input, jv a, jv b) {
|
||||
static jv f_divide(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 (jv_number_value(b) == 0.0 && jv_number_value(a) != 0.0)
|
||||
if (jv_number_value(b) == 0.0)
|
||||
return type_error2(a, b, "cannot be divided because the divisor is zero");
|
||||
jv r = jv_number(jv_number_value(a) / jv_number_value(b));
|
||||
jv_free(a);
|
||||
@@ -396,14 +396,22 @@ static jv f_divide(jq_state *jq, jv input, jv a, jv b) {
|
||||
}
|
||||
}
|
||||
|
||||
#define dtoi(n) ((n) < INTMAX_MIN ? INTMAX_MIN : -(n) < INTMAX_MIN ? INTMAX_MAX : (intmax_t)(n))
|
||||
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) {
|
||||
intmax_t bi = (intmax_t)jv_number_value(b);
|
||||
double na = jv_number_value(a);
|
||||
double nb = jv_number_value(b);
|
||||
if (isnan(na) || isnan(nb)) {
|
||||
jv_free(a);
|
||||
jv_free(b);
|
||||
return jv_number(NAN);
|
||||
}
|
||||
intmax_t bi = dtoi(nb);
|
||||
if (bi == 0)
|
||||
return type_error2(a, b, "cannot be divided (remainder) because the divisor is zero");
|
||||
// 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 r = jv_number(bi == -1 ? 0 : dtoi(na) % bi);
|
||||
jv_free(a);
|
||||
jv_free(b);
|
||||
return r;
|
||||
@@ -411,6 +419,7 @@ static jv f_mod(jq_state *jq, jv input, jv a, jv b) {
|
||||
return type_error2(a, b, "cannot be divided (remainder)");
|
||||
}
|
||||
}
|
||||
#undef dtoi
|
||||
|
||||
static jv f_equal(jq_state *jq, jv input, jv a, jv b) {
|
||||
jv_free(input);
|
||||
|
||||
@@ -172,12 +172,6 @@ int block_is_const(block b) {
|
||||
return (block_is_single(b) && (b.first->op == LOADK || b.first->op == PUSHK_UNDER));
|
||||
}
|
||||
|
||||
int block_is_const_inf(block b) {
|
||||
return (block_is_single(b) && b.first->op == LOADK &&
|
||||
jv_get_kind(b.first->imm.constant) == JV_KIND_NUMBER &&
|
||||
isinf(jv_number_value(b.first->imm.constant)));
|
||||
}
|
||||
|
||||
jv_kind block_const_kind(block b) {
|
||||
assert(block_is_const(b));
|
||||
return jv_get_kind(b.first->imm.constant);
|
||||
|
||||
@@ -22,7 +22,6 @@ block gen_op_simple(opcode op);
|
||||
block gen_const(jv constant);
|
||||
block gen_const_global(jv constant, const char *name);
|
||||
int block_is_const(block b);
|
||||
int block_is_const_inf(block b);
|
||||
jv_kind block_const_kind(block b);
|
||||
jv block_const(block b);
|
||||
block gen_op_target(opcode op, block target);
|
||||
|
||||
Generated
+414
-409
File diff suppressed because it is too large.
Load diff
+10
-5
@@ -230,7 +230,16 @@ static block constant_fold(block a, block b, int op) {
|
||||
case '+': res = jv_number(na + nb); break;
|
||||
case '-': res = jv_number(na - nb); break;
|
||||
case '*': res = jv_number(na * nb); break;
|
||||
case '/': res = jv_number(na / nb); break;
|
||||
case '/':
|
||||
if (nb == 0.0) return gen_noop();
|
||||
res = jv_number(na / nb);
|
||||
break;
|
||||
case '%':
|
||||
#define is_unsafe_to_int_cast(n) (isnan(n) || (n) < INTMAX_MIN || -(n) < INTMAX_MIN)
|
||||
if (is_unsafe_to_int_cast(na) || is_unsafe_to_int_cast(nb) || (intmax_t)nb == 0) return gen_noop();
|
||||
#undef is_unsafe_to_int_cast
|
||||
res = jv_number((intmax_t)na % (intmax_t)nb);
|
||||
break;
|
||||
case EQ: res = (cmp == 0 ? jv_true() : jv_false()); break;
|
||||
case NEQ: res = (cmp != 0 ? jv_true() : jv_false()); break;
|
||||
case '<': res = (cmp < 0 ? jv_true() : jv_false()); break;
|
||||
@@ -463,14 +472,10 @@ Exp "*=" Exp {
|
||||
|
||||
Exp '/' Exp {
|
||||
$$ = gen_binop($1, $3, '/');
|
||||
if (block_is_const_inf($$))
|
||||
FAIL(@$, "Division by zero?");
|
||||
} |
|
||||
|
||||
Exp '%' Exp {
|
||||
$$ = gen_binop($1, $3, '%');
|
||||
if (block_is_const_inf($$))
|
||||
FAIL(@$, "Remainder by zero?");
|
||||
} |
|
||||
|
||||
Exp "/=" Exp {
|
||||
|
||||
+15
-8
@@ -556,9 +556,13 @@ null
|
||||
null
|
||||
172
|
||||
|
||||
[(infinite, -infinite) % (1, -1)]
|
||||
[(infinite, -infinite) % (1, -1, infinite)]
|
||||
null
|
||||
[0,0,0,0]
|
||||
[0,0,0,0,0,-1]
|
||||
|
||||
[nan % 1, 1 % nan | isnan]
|
||||
null
|
||||
[true,true]
|
||||
|
||||
1 + tonumber + ("10" | tonumber)
|
||||
4
|
||||
@@ -1664,18 +1668,21 @@ try (1/.) catch .
|
||||
0
|
||||
"number (1) and number (0) cannot be divided because the divisor is zero"
|
||||
|
||||
0/0, (0 as $x | $x/0) | isnan
|
||||
try (1/0) catch .
|
||||
0
|
||||
true
|
||||
true
|
||||
"number (1) and number (0) cannot be divided because the divisor is zero"
|
||||
|
||||
try (0/0) catch .
|
||||
0
|
||||
"number (0) and number (0) cannot be divided because the divisor is zero"
|
||||
|
||||
try (1%.) catch .
|
||||
0
|
||||
"number (1) and number (0) cannot be divided (remainder) because the divisor is zero"
|
||||
|
||||
%%FAIL
|
||||
1/0
|
||||
jq: error: Division by zero? at <top-level>, line 1:
|
||||
try (1%0) catch .
|
||||
0
|
||||
"number (1) and number (0) cannot be divided (remainder) because the divisor is zero"
|
||||
|
||||
# Basic numbers tests: integers, powers of two
|
||||
[range(-52;52;1)] as $powers | [$powers[]|pow(2;.)|log2|round] == $powers
|
||||
|
||||
+37
-17
@@ -30,47 +30,67 @@ $VALGRIND $Q $JQ -Rne '[inputs] == ["a\u0000b", "c\u0000d", "e"]' $d/input
|
||||
|
||||
# String constant folding (addition only)
|
||||
nref=$($VALGRIND $Q $JQ -n --debug-dump-disasm '"foo"' | wc -l)
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '"foo" + "bar"' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Numeric constant folding (not all ops yet)
|
||||
# Numeric constant folding (binary operators)
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '1+1' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '1-1' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '2*3' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9/3' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9%3' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9==3' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9!=3' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9<3' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9>3' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9<=3' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
n=$($VALGRIND $Q $JQ -n --debug-dump-disasm '9>=3' | wc -l)
|
||||
if [ $n -ne $nref ]; then
|
||||
echo "Constant expression folding for strings didn't work"
|
||||
exit 1
|
||||
echo "Constant expression folding for numbers didn't work"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
## Test JSON sequence support
|
||||
|
||||
Reference in new issue
Block a user