Prevent SquareDigit overflow crash - #108
Closed
krotname wants to merge 1 commit into
Closed
Conversation
Owner
Author
|
Закрываю как superseded by #116: изменения перенесены в одну проверенную ветку, все review findings учтены; локальный mvn verify прошёл полностью. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
squareDigitsmethod concatenated squared digits and calledInteger.parseIntwithout checking range, causingNumberFormatExceptionfor inputs like99999when the constructed decimal exceedsInteger.MAX_VALUE.intinputs while preserving existing behavior for values that fit in a Javaint.Description
MAX_INTstring constant and a pre-parse guard that compares the generated decimal string toInteger.MAX_VALUEby length and lexicographic order insrc/main/java/kyu7/SquareDigit.java.intrange the method now returnsInteger.MAX_VALUEto avoidNumberFormatExceptionwhile keeping representable results unchanged.Integer.parseIntcall on the validated string and remove reliance on unchecked parsing behavior.src/test/java/kyu7/SquareDigitTest.javathat verify a normal case (9119) and the overflow/cap behavior for the reported99999input.Testing
mvn -B -Dtest=SquareDigitTest testand theSquareDigitTestsuite passed (3 tests, 0 failures).mvn -B verifyand the build succeeded, including JaCoCo coverage checks, Checkstyle, PMD, and SpotBugs.Codex Task