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

erans 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 04fc9f46b MATH-1681: Throw exception when parsing fails.
04fc9f46b is described below

commit 04fc9f46bb2c3b8a0f14ed36691d7ff8d7b832a9
Author: Gilles Sadowski <gillese...@gmail.com>
AuthorDate: Fri Jul 25 09:44:51 2025 +0200

    MATH-1681: Throw exception when parsing fails.
---
 .../java/org/apache/commons/math4/legacy/util/ComplexFormat.java | 5 +++--
 .../commons/math4/legacy/util/ComplexFormatAbstractTest.java     | 9 +++++++++
 src/changes/changes.xml                                          | 3 +++
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git 
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/ComplexFormat.java
 
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/ComplexFormat.java
index eeb447cec..a885984c0 100644
--- 
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/ComplexFormat.java
+++ 
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/ComplexFormat.java
@@ -350,7 +350,7 @@ public class ComplexFormat {
     public Complex parse(String source) throws MathParseException {
         ParsePosition parsePosition = new ParsePosition(0);
         Complex result = parse(source, parsePosition);
-        if (parsePosition.getIndex() == 0) {
+        if (result == null) {
             throw new MathParseException(source,
                                          parsePosition.getErrorIndex(),
                                          Complex.class);
@@ -363,7 +363,8 @@ public class ComplexFormat {
      *
      * @param source the string to parse
      * @param pos input/output parsing parameter.
-     * @return the parsed {@link Complex} object.
+     * @return the parsed {@link Complex} object, or {@code null} if
+     * parsing failed.
      */
     public Complex parse(String source, ParsePosition pos) {
         int initialIndex = pos.getIndex();
diff --git 
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java
 
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java
index d737cecc7..a421348d6 100644
--- 
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java
+++ 
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java
@@ -29,6 +29,7 @@ import org.apache.commons.numbers.complex.Complex;
 import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
 import org.apache.commons.math4.legacy.exception.NoDataException;
 import org.apache.commons.math4.legacy.exception.NullArgumentException;
+import org.apache.commons.math4.legacy.exception.MathParseException;
 import org.apache.commons.math4.core.jdkmath.JdkMath;
 
 public abstract class ComplexFormatAbstractTest {
@@ -406,4 +407,12 @@ public abstract class ComplexFormatAbstractTest {
         Assert.assertNull(new ComplexFormat().parse("1 + 1", pos));
         Assert.assertEquals(5, pos.getErrorIndex());
     }
+
+    // MATH-1681.
+    @Test(expected=MathParseException.class)
+    public void testParseMissingImaginaryCharacter() {
+        ComplexFormat format = new ComplexFormat();
+        String invalidComplex = "3 + 5";
+        format.parse(invalidComplex);
+    }
 }
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d22ad612d..919a99459 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -96,6 +96,9 @@ Caveat:
  to support the whole codebase (it was one of the main reasons for
  creating more focused components).
 ">
+      <action dev="erans" type="fix" issue="MATH-1681" due-to="Ruiqi Dong">
+        "ComplexFormat": Throw exception when parsing fails.
+      </action>
       <action dev="erans" type="fix" issue="MATH-1683" due-to="Ruiqi Dong">
         "ElkanKMeansPlusPlusClusterer": More explicit error message.
       </action>

Reply via email to