Skip to content

Prevent SquareDigit overflow crash - #108

Closed
krotname wants to merge 1 commit into
mainfrom
codex/fix-numberformatexception-in-squaredigit
Closed

Prevent SquareDigit overflow crash#108
krotname wants to merge 1 commit into
mainfrom
codex/fix-numberformatexception-in-squaredigit

Conversation

@krotname

@krotname krotname commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Motivation

  • The squareDigits method concatenated squared digits and called Integer.parseInt without checking range, causing NumberFormatException for inputs like 99999 when the constructed decimal exceeds Integer.MAX_VALUE.
  • The change aims to make the method robust for arbitrary int inputs while preserving existing behavior for values that fit in a Java int.

Description

  • Add a MAX_INT string constant and a pre-parse guard that compares the generated decimal string to Integer.MAX_VALUE by length and lexicographic order in src/main/java/kyu7/SquareDigit.java.
  • If the generated result would exceed the int range the method now returns Integer.MAX_VALUE to avoid NumberFormatException while keeping representable results unchanged.
  • Replace the previous direct parse with the guarded Integer.parseInt call on the validated string and remove reliance on unchecked parsing behavior.
  • Add two unit tests in src/test/java/kyu7/SquareDigitTest.java that verify a normal case (9119) and the overflow/cap behavior for the reported 99999 input.

Testing

  • Ran mvn -B -Dtest=SquareDigitTest test and the SquareDigitTest suite passed (3 tests, 0 failures).
  • Ran the full verification mvn -B verify and the build succeeded, including JaCoCo coverage checks, Checkstyle, PMD, and SpotBugs.
  • The added unit tests exercise both the representable-path and the overflow-saturation-path and succeeded under the CI-quality gate.

Codex Task

Comment thread src/main/java/kyu7/SquareDigit.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-numberformatexception-in-squaredigit 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