Harden AverageElement against malformed and oversized input - #95
Closed
krotname wants to merge 1 commit into
Closed
Conversation
Owner
Author
krotname
deleted the
codex/fix-unbounded-stdin-parsing-in-averageelement
branch
August 1, 2026 02:18
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
Integer.valueOf, which allowed EOF, blank, non-numeric tokens, too-few tokens, and extremely long lines to crash or exhaust resources.Description
MAX_INPUT_CHARACTERS = 4096and areadBoundedLine(Reader)helper that returnsnullfor EOF, blank, or oversized lines.solve(Reader, Writer)to use the bounded reader, normalize whitespace withsplit("\\s+"), and silently ignore invalid, blank, undersized, or oversized input by returning early.toList().get(1)with a linear, constant-space algorithm that scans parsed integers once to compute the second-smallest value and throwsIllegalArgumentExceptionfor fewer than two values.src/test/java/coderun/AverageElementTest.javacovering empty/blank input, single-value input, non-numeric tokens, oversized input, duplicates, and multi-value behavior, and keep whitespace-normalization test.Testing
mvn -q -Dtest=coderun.AverageElementTest testand they passed.mvn -q testand it completed successfully.git diff --checkissues were reported after the changes.Codex Task