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 85b9ccd5d reject int overflow of rows * columns in SparseFieldMatrix
85b9ccd5d is described below
commit 85b9ccd5d05e65c127fc71be43d2c2b6e3908826
Author: Javid Khan <[email protected]>
AuthorDate: Sun Oct 4 18:52:20 2026 +0530
reject int overflow of rows * columns in SparseFieldMatrix
---
.../org/apache/commons/math4/legacy/linear/SparseFieldMatrix.java | 8 ++++++++
.../apache/commons/math4/legacy/linear/SparseFieldMatrixTest.java | 7 +++++++
2 files changed, 15 insertions(+)
diff --git
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/SparseFieldMatrix.java
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/SparseFieldMatrix.java
index bd73abe7c..87d182439 100644
---
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/SparseFieldMatrix.java
+++
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/SparseFieldMatrix.java
@@ -18,6 +18,7 @@ package org.apache.commons.math4.legacy.linear;
import org.apache.commons.math4.legacy.core.Field;
import org.apache.commons.math4.legacy.core.FieldElement;
+import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
/**
* Sparse matrix implementation based on an open addressed map.
@@ -62,10 +63,17 @@ public class SparseFieldMatrix<T extends FieldElement<T>>
extends AbstractFieldM
* @param columnDimension Number of columns in the new matrix.
* @throws
org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException
* if row or column dimension is not positive.
+ * @throws NumberIsTooLargeException if the total number of entries of the
+ * matrix is larger than {@code Integer.MAX_VALUE}.
*/
public SparseFieldMatrix(final Field<T> field,
final int rowDimension, final int
columnDimension) {
super(field, rowDimension, columnDimension);
+ final long lRow = rowDimension;
+ final long lCol = columnDimension;
+ if (lRow * lCol >= Integer.MAX_VALUE) {
+ throw new NumberIsTooLargeException(lRow * lCol,
Integer.MAX_VALUE, false);
+ }
this.rows = rowDimension;
this.columns = columnDimension;
entries = new OpenIntToFieldHashMap<>(field);
diff --git
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/SparseFieldMatrixTest.java
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/SparseFieldMatrixTest.java
index 7b41ef873..6ffb49f1a 100644
---
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/SparseFieldMatrixTest.java
+++
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/SparseFieldMatrixTest.java
@@ -21,6 +21,7 @@ import org.junit.Assert;
import org.apache.commons.math4.legacy.core.Field;
import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
import org.apache.commons.math4.legacy.exception.NoDataException;
+import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
import org.apache.commons.math4.legacy.exception.NullArgumentException;
import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
import org.apache.commons.math4.legacy.exception.OutOfRangeException;
@@ -129,6 +130,12 @@ public class SparseFieldMatrixTest {
Assert.assertFalse("testData2 is not square", m2.isSquare());
}
+ /** test dimension product exceeding the integer range */
+ @Test(expected = NumberIsTooLargeException.class)
+ public void testDimensionOverflow() {
+ new SparseFieldMatrix<>(field, 3, Integer.MAX_VALUE);
+ }
+
/** test copy functions */
@Test
public void testCopyFunctions() {