From 4db6dda8f6a74cf5af142fbcbf571941274df5db Mon Sep 17 00:00:00 2001 From: amalsaidov Date: Fri, 13 Nov 2026 10:33:35 +0700 Subject: [PATCH 1/2] fix: catch positive-wrapping overflow in HumanReadableBytes.parse HumanReadableBytes.parse multiplies the parsed number by a unit base and tried to detect overflow with a `value < base` check. That only catches overflows that wrap to a negative value or to something smaller than the base. A large enough input overflows to a positive value that is still greater than or equal to the base, so the check passed and parse returned a silently wrong result instead of reporting the overflow. For example, `20000000000000000k` returned 1553255926290448384. Use Math.multiplyExact so every overflow is caught and reported as a "Number overflow" IAE, consistent with the existing overflow handling. --- .../java/util/common/HumanReadableBytes.java | 11 +++++--- .../util/common/HumanReadableBytesTest.java | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/processing/src/main/java/org/apache/druid/java/util/common/HumanReadableBytes.java b/processing/src/main/java/org/apache/druid/java/util/common/HumanReadableBytes.java index 63e697cc9549..b31c6a20ce64 100644 --- a/processing/src/main/java/org/apache/druid/java/util/common/HumanReadableBytes.java +++ b/processing/src/main/java/org/apache/druid/java/util/common/HumanReadableBytes.java @@ -204,12 +204,15 @@ private static long parseInner(String rawNumber) } try { - long value = Long.parseLong(number.substring(0, lastDigitIndex + 1)) * base; - if (base > 1 && value < base) { - //for base == 1, overflow has been checked in parseLong + final long value = Long.parseLong(number.substring(0, lastDigitIndex + 1)); + try { + // Math.multiplyExact catches every overflow, including those that wrap around to a positive value which the + // previous "value < base" check missed (e.g. 20000000000000000k). + return Math.multiplyExact(value, base); + } + catch (ArithmeticException e) { throw new IAE("Number overflow: %s", rawNumber); } - return value; } catch (NumberFormatException e) { throw new IAE("Invalid format or out of range of long: %s", rawNumber); diff --git a/processing/src/test/java/org/apache/druid/java/util/common/HumanReadableBytesTest.java b/processing/src/test/java/org/apache/druid/java/util/common/HumanReadableBytesTest.java index aa11e1a10ff9..6a94ab45ea8d 100644 --- a/processing/src/test/java/org/apache/druid/java/util/common/HumanReadableBytesTest.java +++ b/processing/src/test/java/org/apache/druid/java/util/common/HumanReadableBytesTest.java @@ -335,6 +335,32 @@ public void testOverflowPiB() Assertions.assertTrue(exception.getMessage().startsWith(OVERFLOW_PREFIX)); } + /** + * The overflow tests above all use the smallest multiplier that overflows, which wraps around to a negative value. + * A large enough multiplier overflows to a positive value instead, which used to slip through the overflow check + * and return a silently wrong result. 20000000000000000 * 1000 = 2e19 is well beyond Long.MAX_VALUE. + */ + @Test + public void testOverflowPositiveWrapK() + { + final IAE exception = Assertions.assertThrows(IAE.class, () -> HumanReadableBytes.parse("20000000000000000k")); + Assertions.assertTrue(exception.getMessage().startsWith(OVERFLOW_PREFIX)); + } + + @Test + public void testOverflowPositiveWrapKiB() + { + final IAE exception = Assertions.assertThrows(IAE.class, () -> HumanReadableBytes.parse("20000000000000000KiB")); + Assertions.assertTrue(exception.getMessage().startsWith(OVERFLOW_PREFIX)); + } + + @Test + public void testOverflowPositiveWrapGiB() + { + final IAE exception = Assertions.assertThrows(IAE.class, () -> HumanReadableBytes.parse("20000000000000000GiB")); + Assertions.assertTrue(exception.getMessage().startsWith(OVERFLOW_PREFIX)); + } + @Test public void testJSON() throws JsonProcessingException { From 93c7f605dc0c4bb49a6e5df4b03bb5de52571145 Mon Sep 17 00:00:00 2001 From: Amal Saidov Date: Mon, 16 Nov 2026 13:04:09 +0700 Subject: [PATCH 2/2] Address review: separate overflow try-catch and parameterize tests - Move the Math.multiplyExact try-catch out of the Long.parseLong try-catch so each guards only its own exception. - Merge the three positive-wrapping overflow tests into one parameterized test over all unit suffixes. - Add a parameterized test covering zero with a unit suffix (0k, 0m, ...), which now returns 0 instead of being rejected as an overflow. --- .../java/util/common/HumanReadableBytes.java | 20 ++++++------ .../util/common/HumanReadableBytesTest.java | 32 ++++++++++--------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/processing/src/main/java/org/apache/druid/java/util/common/HumanReadableBytes.java b/processing/src/main/java/org/apache/druid/java/util/common/HumanReadableBytes.java index b31c6a20ce64..5f9d21c2e275 100644 --- a/processing/src/main/java/org/apache/druid/java/util/common/HumanReadableBytes.java +++ b/processing/src/main/java/org/apache/druid/java/util/common/HumanReadableBytes.java @@ -203,20 +203,22 @@ private static long parseInner(String rawNumber) break; } + final long value; try { - final long value = Long.parseLong(number.substring(0, lastDigitIndex + 1)); - try { - // Math.multiplyExact catches every overflow, including those that wrap around to a positive value which the - // previous "value < base" check missed (e.g. 20000000000000000k). - return Math.multiplyExact(value, base); - } - catch (ArithmeticException e) { - throw new IAE("Number overflow: %s", rawNumber); - } + value = Long.parseLong(number.substring(0, lastDigitIndex + 1)); } catch (NumberFormatException e) { throw new IAE("Invalid format or out of range of long: %s", rawNumber); } + + try { + // Math.multiplyExact catches every overflow, including those that wrap around to a positive value which the + // previous "value < base" check missed (e.g. 20000000000000000k). + return Math.multiplyExact(value, base); + } + catch (ArithmeticException e) { + throw new IAE("Number overflow: %s", rawNumber); + } } public enum UnitSystem diff --git a/processing/src/test/java/org/apache/druid/java/util/common/HumanReadableBytesTest.java b/processing/src/test/java/org/apache/druid/java/util/common/HumanReadableBytesTest.java index 6a94ab45ea8d..63509ac4cd4c 100644 --- a/processing/src/test/java/org/apache/druid/java/util/common/HumanReadableBytesTest.java +++ b/processing/src/test/java/org/apache/druid/java/util/common/HumanReadableBytesTest.java @@ -28,6 +28,8 @@ import org.apache.druid.utils.CollectionUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.util.HashMap; import java.util.Map; @@ -338,27 +340,27 @@ public void testOverflowPiB() /** * The overflow tests above all use the smallest multiplier that overflows, which wraps around to a negative value. * A large enough multiplier overflows to a positive value instead, which used to slip through the overflow check - * and return a silently wrong result. 20000000000000000 * 1000 = 2e19 is well beyond Long.MAX_VALUE. + * and return a silently wrong result. 20000000000000000 multiplied by any unit base is well beyond Long.MAX_VALUE + * (e.g. 20000000000000000 * 1000 = 2e19), so every suffix must report an overflow. */ - @Test - public void testOverflowPositiveWrapK() - { - final IAE exception = Assertions.assertThrows(IAE.class, () -> HumanReadableBytes.parse("20000000000000000k")); - Assertions.assertTrue(exception.getMessage().startsWith(OVERFLOW_PREFIX)); - } - - @Test - public void testOverflowPositiveWrapKiB() + @ParameterizedTest + @ValueSource(strings = {"k", "m", "g", "t", "p", "KiB", "MiB", "GiB", "TiB", "PiB"}) + public void testOverflowPositiveWrap(String suffix) { - final IAE exception = Assertions.assertThrows(IAE.class, () -> HumanReadableBytes.parse("20000000000000000KiB")); + final IAE exception = + Assertions.assertThrows(IAE.class, () -> HumanReadableBytes.parse("20000000000000000" + suffix)); Assertions.assertTrue(exception.getMessage().startsWith(OVERFLOW_PREFIX)); } - @Test - public void testOverflowPositiveWrapGiB() + /** + * Zero with a unit suffix multiplies to 0 and must not be rejected. The previous "value < base" overflow check + * treated 0 as an overflow and threw; after the fix it correctly returns 0. + */ + @ParameterizedTest + @ValueSource(strings = {"0k", "0m", "0g", "0t", "0p", "0KiB", "0MiB", "0GiB", "0TiB", "0PiB"}) + public void testZeroWithUnit(String input) { - final IAE exception = Assertions.assertThrows(IAE.class, () -> HumanReadableBytes.parse("20000000000000000GiB")); - Assertions.assertTrue(exception.getMessage().startsWith(OVERFLOW_PREFIX)); + Assertions.assertEquals(0L, HumanReadableBytes.parse(input)); } @Test