This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git


The following commit(s) were added to refs/heads/master by this push:
     new f8c74b741 Reject malformed input in RealMatrixFormat and 
RealVectorFormat parse (#347)
f8c74b741 is described below

commit f8c74b7419ca4fc421bde2a93ab30a3d39bcb0ca
Author: insaf021 <[email protected]>
AuthorDate: Sun Nov 29 22:52:10 2026 +0530

    Reject malformed input in RealMatrixFormat and RealVectorFormat parse (#347)
---
 .../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 25f24b400..8a02930f3 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 class RealMatrixFormat {
     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 class RealMatrixFormat {
                 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 cc52873ff..f6f42d6fe 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 class RealVectorFormat {
     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 8030cc78c..f84b543e3 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 abstract class RealMatrixFormatAbstractTest {
         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 ccb2ad6a3..91ac98fea 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 abstract class RealVectorFormatAbstractTest {
         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
+            }
+        }
+    }
 }

Reply via email to