Skip to content

Prevent integer overflow in AreSame square comparison - #129

Merged
krotname merged 2 commits into
mainfrom
codex/fix-integer-overflow-in-aresame.comp
Aug 18, 2026
Merged

Prevent integer overflow in AreSame square comparison#129
krotname merged 2 commits into
mainfrom
codex/fix-integer-overflow-in-aresame.comp

Conversation

@krotname

Copy link
Copy Markdown
Owner

Motivation

  • AreSame.comp squared int values using i * i, which silently overflows for |i| > 46340 and can make the comparison return incorrect true results.
  • The change is intended to make the square comparison mathematically correct for all int inputs by avoiding wrapped 32-bit multiplication results.

Description

  • Use mapToLong(i -> (long) i * i) when squaring the first array so the multiplication is performed in 64-bit arithmetic before sorting and comparison.
  • Widen the second stream to long via mapToLong(i -> i) so both sides are compared as long[] values and overflowed int squares cannot be accepted.
  • Add a regression test rejectsOverflowedSquares in AreSameTest that asserts comp(new int[]{46341}, new int[]{-2147479015}) and comp(new int[]{Integer.MIN_VALUE}, new int[]{0}) return false.

Testing

  • Ran mvn -Dtest=AreSameTest test and the test suite executed the updated tests with Tests run: 2, Failures: 0, Errors: 0 indicating success.
  • Ran git diff --check locally to validate the workspace diff contains only the intended changes and reported no whitespace or diff-check issues.

Codex Task

@krotname
krotname merged commit 8e74412 into main Aug 18, 2026
14 checks passed
@krotname
krotname deleted the codex/fix-integer-overflow-in-aresame.comp branch August 18, 2026 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant