Skip to content

Handle overflow when reversing integers - #111

Closed
krotname wants to merge 1 commit into
mainfrom
codex/fix-integer-overflow-in-reverseint
Closed

Handle overflow when reversing integers#111
krotname wants to merge 1 commit into
mainfrom
codex/fix-integer-overflow-in-reverseint

Conversation

@krotname

@krotname krotname commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Motivation

  • The existing ReverseInt.reverse(int) reversed the decimal string and used Integer.parseInt, which throws NumberFormatException when the reversed value exceeds 32-bit signed int bounds.

Description

  • Replace string reversal + Integer.parseInt with parsing the reversed magnitude into a long using Long.parseLong after Math.abs((long) x), then restore the sign and clamp out-of-range results.
  • The method now returns 0 when the reversed value is outside Integer.MIN_VALUE..Integer.MAX_VALUE while preserving previous behavior for in-range results.
  • Updated src/main/java/other/ReverseInt.java to implement the long-based reversal and bound check.
  • Extended src/test/java/other/ReverseIntTest.java with regression cases for a positive overflow (1534236469) and Integer.MIN_VALUE to ensure overflow returns 0.

Testing

  • Ran the focused unit test with mvn -B -Dtest=other.ReverseIntTest test, and the test suite for that class passed (8 tests, 0 failures).
  • Ran the full verification build with mvn -B verify, and the project completed successfully (599 tests run, 0 failures) with Checkstyle, PMD, and SpotBugs analysis passing.

Codex Task

Comment thread src/main/java/other/ReverseInt.java
@krotname

krotname commented Aug 1, 2026

Copy link
Copy Markdown
Owner Author

Закрываю как superseded by #116: изменения перенесены в одну проверенную ветку, все review findings учтены; локальный mvn verify прошёл полностью.

@krotname krotname closed this Aug 1, 2026
@krotname
krotname deleted the codex/fix-integer-overflow-in-reverseint branch August 1, 2026 08:36
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.

2 participants