Skip to content

fix: catch positive-wrapping overflow in HumanReadableBytes.parse - #20894

Merged
FrankChen021 merged 2 commits into
apache:masterfrom
amalsaidov:fix-humanreadablebytes-overflow
Nov 16, 2026
Merged

FrankChen021 merged 2 commits into
apache:masterfrom
amalsaidov:fix-humanreadablebytes-overflow

Conversation

@amalsaidov

Copy link
Copy Markdown
Contributor

Description

HumanReadableBytes.parse multiplies the parsed number by a unit base (for example 1000 for k, 1024 for KiB) and tries to reject overflow with this check:

long value = Long.parseLong(number.substring(0, lastDigitIndex + 1)) * base;
if (base > 1 && value < base) {
  throw new IAE("Number overflow: %s", rawNumber);
}

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:

HumanReadableBytes.parse("20000000000000000k")

20000000000000000 * 1000 = 2e19, which is well beyond Long.MAX_VALUE. Before this change the call returned 1553255926290448384 (the low 64 bits of the product) rather than reporting an overflow. The same happens for the other units, e.g. 20000000000000000KiB and 20000000000000000GiB.

Fixed the overflow detection

Replaced the manual value < base check with Math.multiplyExact, which detects every overflow (including the positive-wrapping case) and is reported as the same "Number overflow" IAE the 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 20000000000000000k that overflow a long are now rejected with a clear error instead of being parsed into a silently incorrect value.


Key changed/added classes in this PR
  • HumanReadableBytes

This PR has:

  • been self-reviewed.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.

AI-assisted; directed, reviewed, and tested by the author (amalsaidov).

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 FrankChen021 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. merge these 3 new test cases together by using one parameterized test over all suffixes

  2. 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 FrankChen021 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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)

@FrankChen021 FrankChen021 added Approval Recommended Used when AI completes a review and reports no findings and removed Approval Recommended Used when AI completes a review and reports no findings labels Nov 13, 2026
- 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.
@amalsaidov

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Addressed both points:

  • Moved the Math.multiplyExact try-catch out of the Long.parseLong try-catch so each one guards only its own exception.
  • Merged the three overflow cases into a single parameterized test over all suffixes, and added a parameterized test for 0k/0m/... which now correctly returns 0.

I'll take a look at #20629 as well for reference.

@FrankChen021
FrankChen021 merged commit f316506 into apache:master Nov 16, 2026
28 checks passed
@github-actions github-actions Bot added this to the 40.0.0 milestone Nov 16, 2026
@amalsaidov

Copy link
Copy Markdown
Contributor Author

Thanks for merging 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants