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 a6109b5d8 Validate direction number degree in SobolSequenceGenerator 
stream parser (#325)
a6109b5d8 is described below

commit a6109b5d800c7bd4ec95736b367e5cb6c9c3d9c6
Author: Javid Khan <[email protected]>
AuthorDate: Mon Aug 24 13:18:34 2026 +0530

    Validate direction number degree in SobolSequenceGenerator stream parser 
(#325)
    
    Add test for out-of-range direction number degree in SobolSequenceGenerator
    
    ---------
    
    Co-authored-by: Alex Herbert <[email protected]>
---
 .../legacy/random/SobolSequenceGenerator.java      |  3 ++
 .../legacy/random/SobolSequenceGeneratorTest.java  | 39 +++++++++++++++++++++-
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git 
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/SobolSequenceGenerator.java
 
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/SobolSequenceGenerator.java
index bf4a2a9ac..58d1d0d22 100644
--- 
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/SobolSequenceGenerator.java
+++ 
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/SobolSequenceGenerator.java
@@ -205,6 +205,9 @@ public class SobolSequenceGenerator implements 
Supplier<double[]> {
                     dim = Integer.parseInt(st.nextToken());
                     if (dim >= 2 && dim <= dimension) { // we have found the 
right dimension
                         final int s = Integer.parseInt(st.nextToken());
+                        if (s < 1 || s > BITS) {
+                            throw new MathParseException(line, lineNumber);
+                        }
                         final int a = Integer.parseInt(st.nextToken());
                         final int[] m = new int[s + 1];
                         for (int i = 1; i <= s; i++) {
diff --git 
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/SobolSequenceGeneratorTest.java
 
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/SobolSequenceGeneratorTest.java
index 39f1f4029..2145a030d 100644
--- 
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/SobolSequenceGeneratorTest.java
+++ 
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/SobolSequenceGeneratorTest.java
@@ -18,8 +18,12 @@ package org.apache.commons.math4.legacy.random;
 
 import org.junit.Assert;
 
+import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 
+import org.apache.commons.math4.legacy.exception.MathParseException;
 import org.apache.commons.math4.legacy.exception.OutOfRangeException;
 import org.junit.Before;
 import org.junit.Test;
@@ -74,7 +78,7 @@ public class SobolSequenceGeneratorTest {
     }
 
     @Test
-    public void testConstructor2() throws Exception{
+    public void testConstructor2() throws IOException {
         try {
             final InputStream is = 
getClass().getResourceAsStream(RESOURCE_NAME);
             new SobolSequenceGenerator(21202, is);
@@ -91,6 +95,39 @@ public class SobolSequenceGeneratorTest {
         }
     }
 
+    @Test
+    public void testConstructorDegreeTooLarge() throws IOException {
+        // direction number degree s = 60 exceeds the BITS (52) entries 
available
+        // per dimension; without range validation this indexes past 
direction[d]
+        // and throws ArrayIndexOutOfBoundsException instead of 
MathParseException.
+        final StringBuilder sb = new StringBuilder("d s a m_i\n2 60 0");
+        for (int i = 0; i < 60; i++) {
+            sb.append(" 1");
+        }
+        final InputStream is = new 
ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8));
+        try {
+            new SobolSequenceGenerator(2, is);
+            Assert.fail("an exception should have been thrown");
+        } catch (MathParseException e) {
+            // expected
+        }
+    }
+
+    @Test
+    public void testConstructorDegreeTooSmall() throws IOException {
+        // direction number degree s = 60 exceeds the BITS (52) entries 
available
+        // per dimension; without range validation this indexes past 
direction[d]
+        // and throws ArrayIndexOutOfBoundsException instead of 
MathParseException.
+        final StringBuilder sb = new StringBuilder("d s a m_i\n2 0 0");
+        final InputStream is = new 
ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8));
+        try {
+            new SobolSequenceGenerator(2, is);
+            Assert.fail("an exception should have been thrown");
+        } catch (MathParseException e) {
+            // expected
+        }
+    }
+
     @Test
     public void testSkip() {
         double[] result = generator.skipTo(5);

Reply via email to