From 870b22c738f51918d268fb9dfac58a5b0075e522 Mon Sep 17 00:00:00 2001 From: Mahammad Insaf Date: Mon, 2 Nov 2026 19:02:14 +0530 Subject: [PATCH] reject malformed input in RealMatrixFormat and RealVectorFormat parse --- .../math4/legacy/linear/RealMatrixFormat.java | 5 ++-- .../math4/legacy/linear/RealVectorFormat.java | 2 +- .../linear/RealMatrixFormatAbstractTest.java | 27 +++++++++++++++++++ .../linear/RealVectorFormatAbstractTest.java | 13 +++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/RealMatrixFormat.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/RealMatrixFormat.java index 25f24b4004..8a02930f3b 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/RealMatrixFormat.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/RealMatrixFormat.java @@ -283,7 +283,7 @@ public StringBuffer format(RealMatrix matrix, StringBuffer toAppendTo, public RealMatrix parse(String source) { final ParsePosition parsePosition = new ParsePosition(0); final RealMatrix result = parse(source, parsePosition); - if (parsePosition.getIndex() == 0) { + if (result == null) { throw new MathParseException(source, parsePosition.getErrorIndex(), Array2DRowRealMatrix.class); @@ -348,7 +348,8 @@ public RealMatrix parse(String source, ParsePosition pos) { if (component != null) { rowComponents.add(component); } else { - if (rowComponents.isEmpty()) { + if (rowComponents.isEmpty() && + (matrix.isEmpty() || trimmedRowPrefix.isEmpty())) { loop = false; } else { // invalid component diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/RealVectorFormat.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/RealVectorFormat.java index cc52873ff2..f6f42d6fe8 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/RealVectorFormat.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/RealVectorFormat.java @@ -222,7 +222,7 @@ public StringBuffer format(RealVector vector, StringBuffer toAppendTo, public ArrayRealVector parse(String source) { final ParsePosition parsePosition = new ParsePosition(0); final ArrayRealVector result = parse(source, parsePosition); - if (parsePosition.getIndex() == 0) { + if (result == null) { throw new MathParseException(source, parsePosition.getErrorIndex(), ArrayRealVector.class); diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealMatrixFormatAbstractTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealMatrixFormatAbstractTest.java index 8030cc78cd..f84b543e3c 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealMatrixFormatAbstractTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealMatrixFormatAbstractTest.java @@ -375,4 +375,31 @@ public void testParseEmptySuffixAtEndOfInput() { Assert.assertEquals(MatrixUtils.createRealMatrix(new double[][] {{1, 2}, {3, 4}}), parsed); Assert.assertEquals(10, pos.getIndex()); } + + @Test + public void testParseEmptyRow() { + for (final String source : new String[] {"{{1},{}}", "{{1},{}", "{{1, 2}, {}}", "{{1}, {2}, { }}"}) { + final ParsePosition pos = new ParsePosition(0); + Assert.assertNull("Should not parse <" + source + ">", realMatrixFormat.parse(source, pos)); + Assert.assertEquals(0, pos.getIndex()); + } + } + + @Test + public void testParseEmptySuffixClosingRowSeparator() { + final RealMatrix parsed = new RealMatrixFormat("", "", "", "", "; ", ", ").parse("1, 2; "); + Assert.assertEquals(MatrixUtils.createRealMatrix(new double[][] {{1, 2}}), parsed); + } + + @Test + public void testParseFailureThrows() { + for (final String source : new String[] {"{}", "{{1},}", "{{1},{2}", "{{1}{2}}", "{{1, 1 1}}", "{{1}"}) { + try { + realMatrixFormat.parse(source); + Assert.fail("Expecting MathParseException for <" + source + ">"); + } catch (MathParseException pe) { + // expected behavior + } + } + } } diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealVectorFormatAbstractTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealVectorFormatAbstractTest.java index ccb2ad6a39..91ac98fea5 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealVectorFormatAbstractTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/RealVectorFormatAbstractTest.java @@ -351,4 +351,17 @@ public void testForgottenSuffix() { Assert.assertNull("Should not parse <"+source+">",new RealVectorFormat().parse(source, pos)); Assert.assertEquals(8, pos.getErrorIndex()); } + + @Test + public void testParseFailureThrows() { + final RealVectorFormat format = new RealVectorFormat(); + for (final String source : new String[] {"{1", "{1; 2", "{1 2}", "{1; 1 1}"}) { + try { + format.parse(source); + Assert.fail("Expecting MathParseException for <" + source + ">"); + } catch (MathParseException pe) { + // expected behavior + } + } + } }