From 47ec41e358c3ba713a0e28186ec73d00d3372904 Mon Sep 17 00:00:00 2001 From: amalsaidov <58995855+amalsaidov@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:36:27 +0700 Subject: [PATCH 1/2] Make % (mod) on BigInteger operands use truncated remainder The BigInteger branch of JexlArithmetic.mod used BigInteger.mod(), which returns a non-negative result and throws ArithmeticException for a negative divisor. The long, double and BigDecimal branches all use the truncated remainder (sign follows the dividend), as does Java's % operator and JexlArithmetic.divide (which truncates toward zero). As a result, the value of a % expression depended on the runtime representation of its operands: -39 % 2 -> -1 -39H % 2H -> 1 39 % -2 -> 1 39H % -2H -> throws "modulus not positive" Switch the BigInteger branch to BigInteger.remainder() so that % is consistent across all numeric types and with integer division. Add a regression test covering negative dividends and divisors. --- .../apache/commons/jexl3/JexlArithmetic.java | 2 +- .../apache/commons/jexl3/ArithmeticTest.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java b/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java index 18bff9cf8..8d2e03771 100644 --- a/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java +++ b/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java @@ -1387,7 +1387,7 @@ public Object mod(final Object left, final Object right) { if (BigInteger.ZERO.equals(r)) { throw new ArithmeticException("%"); } - final BigInteger result = l.mod(r); + final BigInteger result = l.remainder(r); return narrowBigInteger(left, right, result); } diff --git a/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java b/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java index 8854bcb74..da06a0520 100644 --- a/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java +++ b/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java @@ -1853,6 +1853,27 @@ void testModEdge() { assertNullOperand(() -> jexla.divide(null, null)); } + @Test + void testModBigIntegerNegative() { + // '%' on BigInteger operands must use the truncated remainder (the sign follows the + // dividend), consistent with the long, double and BigDecimal code paths and with Java's + // '%' operator. Previously the BigInteger branch used BigInteger.mod(), which returns a + // non-negative result and throws for a negative divisor, so '-39 % 2' depended on whether + // the operands were represented as long or BigInteger. + final long[][] cases = { { -39, 2 }, { 39, -2 }, { -39, -2 }, { 39, 2 }, { -7, 3 }, { 7, -3 } }; + for (final long[] c : cases) { + final long x = c[0]; + final long y = c[1]; + final Number longResult = (Number) jexla.mod(x, y); + final Number bigResult = (Number) jexla.mod(BigInteger.valueOf(x), BigInteger.valueOf(y)); + assertEquals(x % y, longResult.longValue()); + assertEquals(x % y, bigResult.longValue(), + "BigInteger '%' should match the long/Java result for " + x + " % " + y); + } + // A negative divisor must not throw for BigInteger operands (the long path does not throw). + assertEquals(1L, ((Number) jexla.mod(BigInteger.valueOf(39), BigInteger.valueOf(-2))).longValue()); + } + /** * JEXL-156. */ From 929e05087911aade796551eb4afd0335771f0828 Mon Sep 17 00:00:00 2001 From: henrib Date: Wed, 2 Dec 2026 16:06:34 +0100 Subject: [PATCH 2/2] JEXL-478: Add changes.xml entry Co-Authored-By: Claude Fable 5.2 --- src/changes/changes.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 71e65b990..723d3c955 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -29,6 +29,7 @@ + Modulo (%) on BigInteger operands is inconsistent with other numeric types for negative operands. Interpolation strings are parsed with the engine features instead of those of their script. A jexl.options pragma modifies the engine options when evaluated with a plain context.