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 55370d04f MATH-1688: Reject malformed grouping separators in
ComplexFormat (#330)
55370d04f is described below
commit 55370d04f8d617c7e5bbbdd0df57184401d349ac
Author: Efe <[email protected]>
AuthorDate: Mon Oct 26 11:12:53 2026 +0300
MATH-1688: Reject malformed grouping separators in ComplexFormat (#330)
MATH-1688: Reject invalid grouping separators in complex parsing
Signed-off-by: Efe Gökdemir <[email protected]>
---
.../commons/math4/legacy/util/ComplexFormat.java | 31 ++++++++++++++++++----
.../legacy/util/ComplexFormatAbstractTest.java | 15 +++++++++++
2 files changed, 41 insertions(+), 5 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 a885984c0..217148caa 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
@@ -37,7 +37,7 @@ import
org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
*/
public class ComplexFormat {
- /** The default imaginary character. */
+ /** The default imaginary character. */
private static final String DEFAULT_IMAGINARY_CHARACTER = "i";
/** The notation used to signify the imaginary part of the complex number.
*/
private final String imaginaryCharacter;
@@ -52,7 +52,7 @@ public class ComplexFormat {
*/
public ComplexFormat() {
this.imaginaryCharacter = DEFAULT_IMAGINARY_CHARACTER;
- this.imaginaryFormat = CompositeFormat.getDefaultNumberFormat();
+ this.imaginaryFormat = getDefaultNumberFormat();
this.realFormat = imaginaryFormat;
}
@@ -93,6 +93,27 @@ public class ComplexFormat {
this.realFormat = realFormat;
}
+ /**
+ * Creates the default number format used by complex values.
+ *
+ * @return a number format that does not accept grouping separators.
+ */
+ private static NumberFormat getDefaultNumberFormat() {
+ return getDefaultNumberFormat(Locale.getDefault());
+ }
+
+ /**
+ * Creates the default number format used by complex values.
+ *
+ * @param locale the locale used by the format.
+ * @return a number format that does not accept grouping separators.
+ */
+ private static NumberFormat getDefaultNumberFormat(final Locale locale) {
+ final NumberFormat format =
CompositeFormat.getDefaultNumberFormat(locale);
+ format.setGroupingUsed(false);
+ return format;
+ }
+
/**
* Create an instance with a custom imaginary character, and the default
* number format for both real and imaginary parts.
@@ -104,7 +125,7 @@ public class ComplexFormat {
*/
public ComplexFormat(String imaginaryCharacter)
throws NullArgumentException, NoDataException {
- this(imaginaryCharacter, CompositeFormat.getDefaultNumberFormat());
+ this(imaginaryCharacter, getDefaultNumberFormat());
}
/**
@@ -311,7 +332,7 @@ public class ComplexFormat {
* @return the complex format specific to the given locale.
*/
public static ComplexFormat getInstance(Locale locale) {
- NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale);
+ NumberFormat f = getDefaultNumberFormat(locale);
return new ComplexFormat(f);
}
@@ -327,7 +348,7 @@ public class ComplexFormat {
*/
public static ComplexFormat getInstance(String imaginaryCharacter, Locale
locale)
throws NullArgumentException, NoDataException {
- NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale);
+ NumberFormat f = getDefaultNumberFormat(locale);
return new ComplexFormat(imaginaryCharacter, f);
}
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 d483982db..37cffdfd1 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
@@ -444,4 +444,19 @@ public abstract class ComplexFormatAbstractTest {
String invalidComplex = "3 + 5";
format.parse(invalidComplex);
}
+
+ // MATH-1688.
+ @Test
+ public void testParseRejectsInvalidGroupingSeparators() {
+ final ComplexFormat format = new ComplexFormat();
+ final String[] invalid = {",,7+,,,2i", ",8+,,3i", ",7", "7,,8"};
+ for (final String source : invalid) {
+ try {
+ format.parse(source);
+ Assert.fail("Expected parsing to reject: " + source);
+ } catch (MathParseException expected) {
+ // Expected.
+ }
+ }
+ }
}