Skip to content

reject malformed input in RealMatrixFormat and RealVectorFormat parse - #347

Open
insaf021 wants to merge 1 commit into
apache:masterfrom
insaf021:realmatrixformat-reject-malformed
Open

insaf021 wants to merge 1 commit into
apache:masterfrom
insaf021:realmatrixformat-reject-malformed

Conversation

@insaf021

@insaf021 insaf021 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Observed on master with the default formats:

new RealMatrixFormat().parse("{{1,2},{}}") -> Array2DRowRealMatrix{{1.0,2.0}}
new RealMatrixFormat().parse("{{1},{2}")   -> null
new RealVectorFormat().parse("{1;2")       -> null

Two separate paths let malformed text through.

parse(String) is documented to throw MathParseException, but both classes detect failure with parsePosition.getIndex() == 0. The parseFixedstring failure paths in parse(String, ParsePosition) leave the index past whatever was consumed, so the caller gets null instead of the exception. ComplexFormat already tests result == null; the same test is now used here.

In RealMatrixFormat, a row that has its prefix but no component is treated as the end of the matrix, and the unconsumed } is then matched as the matrix suffix. The empty row disappears and the result has the wrong dimensions. Once a row has been opened after earlier rows it now needs a component, so it fails like any other invalid component. Formats with an empty row prefix have no opened row to reject and still accept a closing row separator; a test pins that.

Regression tests in RealMatrixFormatAbstractTest and RealVectorFormatAbstractTest fail without the patch. mvn clean verify on commons-math-legacy and its dependencies passes on JDK 17 (3278 tests in legacy), as does checkstyle:check. This is separate from #338, which fixed the CompositeFormat cursor helpers.

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.

1 participant