Skip to content

Fix MathUtils ceilDiv integer overflow#25876

Open
hiSandog wants to merge 1 commit into
apache:masterfrom
hiSandog:fix/mathutils-ceildiv-overflow-20260527
Open

Fix MathUtils ceilDiv integer overflow#25876
hiSandog wants to merge 1 commit into
apache:masterfrom
hiSandog:fix/mathutils-ceildiv-overflow-20260527

Conversation

@hiSandog
Copy link
Copy Markdown
Contributor

Motivation

MathUtils.ceilDiv(int, int) negated the dividend before calling Math.floorDiv. For Integer.MIN_VALUE, that negation overflows and can return the wrong value for positive divisors such as 2.

Modifications

Compute the quotient and remainder directly, then round up only when the operands have the same sign and there is a non-zero remainder. Added coverage for negative operands and Integer.MIN_VALUE boundaries.

Verifications

  • git diff --check
  • ./gradlew :pulsar-client:test --tests org.apache.pulsar.client.util.MathUtilsTest (did not start locally: no Java Runtime is installed in this environment)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant