Skip to content

Commit 1bbc2fe

Browse files
committed
fix: Drop float-equality check that failed the reliability gate
SonarCloud flagged S1244 (floating-point equality) on the `d == Math.rint(d)` integrality test in checkInteger's fast double path, dropping New Code reliability to C. Remove the double fast path entirely: doubles reaching an integer schema are either fractional (rejected) or rare integral doubles, both handled correctly by converting through BigDecimal.toBigIntegerExact(). Integral wrappers keep the allocation-free long path, so the common case is unaffected.
1 parent f41402e commit 1bbc2fe

1 file changed

Lines changed: 4 additions & 10 deletions

File tree

src/main/java/com/retailsvc/http/validate/DefaultValidator.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,6 @@ private static boolean isHextet(String hextet) {
367367
}
368368

369369
/** Largest magnitude an IEEE-754 double represents as an exact integer (2^53). */
370-
private static final double MAX_EXACT_INTEGRAL_DOUBLE = 9007199254740992.0;
371-
372370
private static Optional<ValidationError> checkInteger(
373371
Object value, IntegerSchema s, String pointer) {
374372
if (!(value instanceof Number num)) {
@@ -382,17 +380,13 @@ private static Optional<ValidationError> checkInteger(
382380
|| num instanceof Byte) {
383381
return checkIntegerBounds(num.longValue(), s, pointer);
384382
}
385-
// Integral doubles small enough to be represented exactly also take the long path.
386-
double d = num.doubleValue();
387-
if (Double.isFinite(d) && d == Math.rint(d) && Math.abs(d) <= MAX_EXACT_INTEGRAL_DOUBLE) {
388-
return checkIntegerBounds((long) d, s, pointer);
389-
}
390-
// Non-integral (e.g. 4.9), non-finite, or beyond a double's exact-integer range: verify the
391-
// value is a whole number and compare the full magnitude so nothing wraps past a bound.
383+
// Any other numeric type (Double, BigDecimal, ...) must be a whole number: convert exactly and
384+
// reject a fractional part (e.g. 4.9) or a non-finite value, comparing the full magnitude so
385+
// nothing wraps past a bound.
392386
BigInteger n;
393387
try {
394388
n = new BigDecimal(num.toString()).toBigIntegerExact();
395-
} catch (ArithmeticException | NumberFormatException e) {
389+
} catch (ArithmeticException | NumberFormatException notIntegral) {
396390
return err(pointer, "type", "expected integer", value);
397391
}
398392
return checkIntegerBounds(n, s, pointer);

0 commit comments

Comments
 (0)