Skip to content

Harden Solution2 stdin handling - #89

Closed
krotname wants to merge 1 commit into
mainfrom
codex/propose-fix-for-unbounded-stdin-processing
Closed

Harden Solution2 stdin handling#89
krotname wants to merge 1 commit into
mainfrom
codex/propose-fix-for-unbounded-stdin-processing

Conversation

@krotname

Copy link
Copy Markdown
Owner

Motivation

  • Prevent uncaught NullPointerException when the declared stdin line count does not match supplied input.
  • Avoid unbounded memory allocation from accumulating all outputs in a single StringBuilder, which can lead to OOM for attacker-controlled large input.
  • Enforce token and numeric validation to avoid malformed input being silently processed or causing unexpected behavior.

Description

  • Added MAX_LINE_COUNT limit and readLineCount, readInputLine, and nextToken helpers to validate the declared line count, EOF, and token availability in src/main/java/algorithms/sprint1/Solution2.java.
  • Replaced output accumulation with streaming to stdout using a PrintWriter to eliminate unbounded StringBuilder growth and preserved the expected trailing blank line.
  • Validated token counts and numeric parsing, replaced addition with Math.addExact for safer arithmetic, and throw IllegalArgumentException on malformed input.
  • Added regression tests in src/test/java/algorithms/AlgorithmCliTest.java to assert that missing input lines and oversized line counts are rejected.

Testing

  • Ran mvn -q -Dtest=AlgorithmCliTest test, which passed after the fix.
  • Ran mvn -q test, which completed successfully.
  • Ran mvn -q verify, which completed successfully and includes the new integration checks for Solution2.

Codex Task

@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 73.07692% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/main/java/algorithms/sprint1/Solution2.java 73.07% 3 Missing and 4 partials ⚠️

📢 Thoughts on this report? Let us know!

@sonarqubecloud

Copy link
Copy Markdown

@krotname

krotname commented Aug 1, 2026

Copy link
Copy Markdown
Owner Author

Superseded by the validated combined merge in #115: #115

@krotname krotname closed this Aug 1, 2026
@krotname
krotname deleted the codex/propose-fix-for-unbounded-stdin-processing branch August 1, 2026 02:18
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