Skip to content

fix end-of-input handling in CompositeFormat cursor helpers - #338

Merged
aherbert merged 1 commit into
apache:masterfrom
insaf021:compositeformat-end-of-input
Oct 26, 2026
Merged

aherbert merged 1 commit into
apache:masterfrom
insaf021:compositeformat-end-of-input

Conversation

@insaf021

Copy link
Copy Markdown
Contributor

Observed on master with the default formats:

new ComplexFormat().parse("2i")     -> (2.0, 0.0)
new ComplexFormat().parse("1x")     -> (1.0, 0.0)
new ComplexFormat().parse("1 +")    -> (1.0, 0.0)
new RealMatrixFormat().parse("{{1}") -> Array2DRowRealMatrix{{1.0}}

Malformed input is accepted here and becomes a wrong value instead of a MathParseException.

The root cause is two end-of-input mistakes in the CompositeFormat cursor helpers. parseNextCharacter decides whether it found a character by testing index < n after the post-increment, so a non-whitespace character in the last position is consumed but reported as 0, which ComplexFormat reads as "no imaginary part". parseAndIgnoreWhitespace always rewinds by one, so at end of input it steps back onto the previous character; in RealMatrixFormat the row suffix } is then matched a second time as the matrix suffix.

The fix tests the character itself in parseNextCharacter and rewinds only when something was consumed. parseFixedstring previously refused any literal at end of input, and formats with an empty suffix only parsed because of the bogus rewind, so it now lets an empty literal match there. Existing error indices (e.g. testForgottenSuffix) are unchanged. This is separate from #326, which fixed special-value matching in parseNumber.

Regression tests are in ComplexFormatAbstractTest and RealMatrixFormatAbstractTest and fail without the patch. mvn -pl commons-math-legacy passes on JDK 21 (checkstyle, PMD, SpotBugs, tests).

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.22%. Comparing base (f554608) to head (6ebdc5a).
⚠️ Report is 391 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #338      +/-   ##
============================================
+ Coverage     86.54%   87.22%   +0.68%     
+ Complexity     9787       91    -9696     
============================================
  Files           532      499      -33     
  Lines         35516    33519    -1997     
  Branches       6194     5848     -346     
============================================
- Hits          30738    29238    -1500     
+ Misses         3518     3162     -356     
+ Partials       1260     1119     -141     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aherbert
aherbert merged commit 8d39db1 into apache:master Oct 26, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants