You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2021/10/16 12:14:41 UTC

[sis] 04/04: Fix an integer overflow in call to `divisors(Integer.MAX_VALUE)`.

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

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit ac3c44d877c75729ce3d4df227f7ceb3b59729f0
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Sat Oct 16 12:22:50 2021 +0200

    Fix an integer overflow in call to `divisors(Integer.MAX_VALUE)`.
---
 .../src/main/java/org/apache/sis/math/MathFunctions.java         | 9 ++++++---
 .../src/main/java/org/apache/sis/math/package-info.java          | 2 +-
 .../src/test/java/org/apache/sis/math/MathFunctionsTest.java     | 6 +++++-
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java b/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
index a93c9f2..d320b02 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
@@ -31,6 +31,7 @@ import static java.lang.Float.floatToRawIntBits;
 import static java.lang.Double.longBitsToDouble;
 import static java.lang.Double.doubleToLongBits;
 import static java.lang.Double.doubleToRawLongBits;
+import static org.apache.sis.internal.jdk9.JDK9.multiplyFull;
 import static org.apache.sis.internal.util.Numerics.SIGN_BIT_MASK;
 import static org.apache.sis.internal.util.Numerics.SIGNIFICAND_SIZE;
 
@@ -61,7 +62,7 @@ import static org.apache.sis.internal.util.Numerics.SIGNIFICAND_SIZE;
  *
  * @author  Martin Desruisseaux (MPO, IRD, Geomatys)
  * @author  Johann Sorel (Geomatys)
- * @version 1.1
+ * @version 1.2
  *
  * @see DecimalFunctions
  * @see org.apache.sis.util.Numbers
@@ -880,7 +881,7 @@ testNextNumber:         while (true) {      // Simulate a "goto" statement (usua
          * values before that point, i.e. if n=p₁⋅p₂ and p₂ is greater than `sqrt`, than p₁
          * must be lower than `sqrt`.
          */
-        for (int p,i=0; (p=primeNumberAt(i))*p <= number; i++) {
+        for (int p,i=0; multiplyFull(p=primeNumberAt(i), p) <= number; i++) {
             if (number % p == 0) {
                 if (count == divisors.length) {
                     divisors = Arrays.copyOf(divisors, count*2);
@@ -913,7 +914,9 @@ testNextNumber:         while (true) {      // Simulate a "goto" statement (usua
         for (int i=1; i<count; i++) {
             d1 = divisors[i];
             for (int j=i; j<count; j++) {
-                d2 = d1 * divisors[j];
+                final long m = multiplyFull(d1, divisors[j]);
+                if (m > number) break;
+                d2 = (int) m;
                 if (number % d2 == 0) {
                     int p = Arrays.binarySearch(divisors, j, count, d2);
                     if (p < 0) {
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/package-info.java b/core/sis-utility/src/main/java/org/apache/sis/math/package-info.java
index 4e54a86..5f2ad83 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/package-info.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/package-info.java
@@ -40,7 +40,7 @@
  * </ul>
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.8
+ * @version 1.2
  * @since   0.3
  * @module
  */
diff --git a/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java b/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
index 6b253d1..64c74d3 100644
--- a/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
+++ b/core/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
@@ -35,7 +35,7 @@ import static org.apache.sis.internal.util.Numerics.SIGNIFICAND_SIZE;
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @author  Johann Sorel (Geomatys)
- * @version 1.1
+ * @version 1.2
  * @since   0.3
  * @module
  */
@@ -438,6 +438,10 @@ public final strictfp class MathFunctionsTest extends TestCase {
             78, 100, 104, 120, 130, 150, 156, 195, 200, 260, 300, 312, 325, 390, 520, 600, 650,
             780, 975, 1300, 1560, 1950, 2600, 3900, 7800
         }, divisors(7800));
+
+        assertArrayEquals(new int[] {
+            1, Integer.MAX_VALUE
+        }, divisors(Integer.MAX_VALUE));
     }
 
     /**