Skip to content

Handle malformed RPN calculator input safely - #91

Closed
krotname wants to merge 1 commit into
mainfrom
codex/fix-calculator-to-handle-malformed-input
Closed

Handle malformed RPN calculator input safely#91
krotname wants to merge 1 commit into
mainfrom
codex/fix-calculator-to-handle-malformed-input

Conversation

@krotname

@krotname krotname commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Motivation

  • The CLI Calculator previously assumed well-formed RPN tokens and performed unchecked stack pops, division, and integer parsing which allowed malformed stdin (e.g. +, empty input, 1 0 /, abc, or extra operands) to throw runtime exceptions and terminate the process. The change prevents these crash/availability paths by validating untrusted input.

Description

  • Validate operand counts before applying an operator by checking st.size() < 2 and reject with a controlled error instead of performing unchecked pop() calls in eval (src/main/java/algorithms/sprint2/Calculator.java).
  • Detect division-by-zero before calling Math.floorDiv and reject with a clear error message.
  • Convert invalid numeric tokens by wrapping parseInt in a try/catch and rethrowing as IllegalArgumentException, and require the final stack size to be exactly one before returning the result.
  • Handle IllegalArgumentException at the CLI boundary in run() by printing a short diagnostic to stderr and returning instead of allowing stack/parse/arithmetic exceptions to propagate.
  • Add regression coverage for malformed inputs by adding a parameterized test to src/test/java/algorithms/AlgorithmCliTest.java that verifies the Calculator rejects +, empty input, 1 0 /, abc, and an extra-operand case.

Testing

  • Ran mvn -q -Dtest=algorithms.AlgorithmCliTest test and the test suite for the modified integration case completed successfully.
  • Ran mvn -q test to execute the full test suite and it completed without failures.
  • Ran mvn -q checkstyle:check and code passes the project's checkstyle rules.
  • Ran git diff --check to ensure no whitespace or patch issues and it reported clean results.

Codex Task

@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/fix-calculator-to-handle-malformed-input 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