Repository navigation
fix: catch positive-wrapping overflow in HumanReadableBytes.parse - #20894
Conversation
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.
FrankChen021
left a comment
There was a problem hiding this comment.
Thanks for the fix. I already fixed this problem in PR: #20629
but since it has not been merged, addressing this problem separately is also acceptable. You can also refer to the change in that PR for the reference of changes.
| 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 { |
There was a problem hiding this comment.
move this new try-catch out of current try-catch
| * and return a silently wrong result. 20000000000000000 * 1000 = 2e19 is well beyond Long.MAX_VALUE. | ||
| */ | ||
| @Test | ||
| public void testOverflowPositiveWrapK() |
There was a problem hiding this comment.
-
merge these 3 new test cases together by using one parameterized test over all suffixes
-
add a new parameterized test to test 0k, 0m (and all unit suffixes), this is a special case which after your fixes should return 0 but previously it throws exception
FrankChen021
left a comment
There was a problem hiding this comment.
🟢 Approval recommended
No actionable issues found in this review. The change correctly replaces incomplete overflow detection with Math.multiplyExact while preserving the existing error contract.
Reviewed 2 of 2 changed files.
Validation: git diff --check passed.
This is an automated review by Codex GPT-5.6-Luna(max)
- 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.
|
Thanks for the review! Addressed both points:
I'll take a look at #20629 as well for reference. |
|
Thanks for merging 🙏 |
Description
HumanReadableBytes.parsemultiplies the parsed number by a unit base (for example1000fork,1024forKiB) and tries to reject overflow with this check:That check only catches overflows that wrap around to a negative value, or to a positive value smaller than
base. When the multiplication overflows to a positive value that is still>= base, the check passes and the method returns a silently wrong result instead of reporting the overflow.For example:
20000000000000000 * 1000 = 2e19, which is well beyondLong.MAX_VALUE. Before this change the call returned1553255926290448384(the low 64 bits of the product) rather than reporting an overflow. The same happens for the other units, e.g.20000000000000000KiBand20000000000000000GiB.Fixed the overflow detection
Replaced the manual
value < basecheck withMath.multiplyExact, which detects every overflow (including the positive-wrapping case) and is reported as the same"Number overflow"IAEthe method already used. The existing boundary overflow tests (Long.MAX_VALUE / base + 1) still pass, and three new tests cover the positive-wrapping case.Release note
Fixed: Byte-size config values such as
20000000000000000kthat overflow alongare now rejected with a clear error instead of being parsed into a silently incorrect value.Key changed/added classes in this PR
HumanReadableBytesThis PR has:
AI-assisted; directed, reviewed, and tested by the author (amalsaidov).