insaf021 opened a new pull request, #338:
URL: https://github.com/apache/commons-math/pull/338

   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).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to