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:
itchyny authored and Nico Williams committed 2023-07-31 15:39:13 -05:00
1 parent 6716e23ae6
commit 0f80921268
7 files changed
+488 -449

No files matched your search

+37 -17
View File
@@ -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