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 543113b57838bab6f56689d80dae2956dcdf3b89
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Wed Oct 17 18:44:44 2018 +0200

    Define a constant for the limit in the number of dimensions that can handle 
Apache SIS.
---
 .../java/org/apache/sis/io/wkt/GeodeticObjectParser.java  |  2 +-
 .../java/org/apache/sis/coverage/grid/GridExtent.java     |  4 +++-
 .../sis/referencing/operation/matrix/GeneralMatrix.java   |  5 +++--
 .../main/java/org/apache/sis/internal/util/Numerics.java  | 15 +++++++++++++++
 .../java/org/apache/sis/internal/storage/csv/Store.java   |  7 ++++---
 5 files changed, 26 insertions(+), 7 deletions(-)

diff --git 
a/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/GeodeticObjectParser.java
 
b/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/GeodeticObjectParser.java
index 3f6b883..5c14e27 100644
--- 
a/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/GeodeticObjectParser.java
+++ 
b/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/GeodeticObjectParser.java
@@ -741,7 +741,7 @@ class GeodeticObjectParser extends MathTransformParser 
implements Comparator<Coo
                                 new String[] {WKTKeywords.CS, type}, 
element.offset);
                     }
                 }
-                if (dimension <= 0 || dimension > 1000) {       // Arbitrary 
upper limit against badly formed CS.
+                if (dimension <= 0 || dimension >= 
Numerics.MAXIMUM_MATRIX_SIZE) {
                     final short key;
                     final Object[] args;
                     if (dimension <= 0) {
diff --git 
a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java 
b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
index 9ebd773..f4055a9 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
@@ -37,6 +37,7 @@ import org.apache.sis.util.resources.Vocabulary;
 import org.apache.sis.util.collection.WeakValueHashMap;
 import org.apache.sis.internal.metadata.AxisDirections;
 import org.apache.sis.internal.raster.Resources;
+import org.apache.sis.internal.util.Numerics;
 import org.apache.sis.geometry.AbstractEnvelope;
 import org.apache.sis.geometry.GeneralEnvelope;
 import org.apache.sis.geometry.Envelopes;
@@ -129,7 +130,8 @@ public class GridExtent implements Serializable {
      * @throws IllegalArgumentException if the given number of dimensions is 
excessive.
      */
     private static long[] allocate(final int dimension) throws 
IllegalArgumentException {
-        if (dimension > Integer.MAX_VALUE / 2) {
+        if (dimension >= Numerics.MAXIMUM_MATRIX_SIZE) {
+            // Actually the real limit is Integer.MAX_VALUE / 2, but a value 
too high is likely to be an error.
             throw new 
IllegalArgumentException(Errors.format(Errors.Keys.ExcessiveNumberOfDimensions_1,
 dimension));
         }
         return new long[dimension << 1];
diff --git 
a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
 
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
index 6978537..adb0af6 100644
--- 
a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
+++ 
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/GeneralMatrix.java
@@ -21,6 +21,7 @@ import org.opengis.referencing.operation.Matrix;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.resources.Errors;
+import org.apache.sis.internal.util.Numerics;
 import org.apache.sis.internal.util.DoubleDouble;
 import org.apache.sis.internal.referencing.ExtendedPrecisionMatrix;
 
@@ -199,8 +200,8 @@ class GeneralMatrix extends MatrixSIS implements 
ExtendedPrecisionMatrix {
      * Ensures that the given matrix size is valid for this {@code 
GeneralMatrix} implementation.
      */
     private static void ensureValidSize(final int numRow, final int numCol) {
-        ArgumentChecks.ensureBetween("numRow", 1, Short.MAX_VALUE, numRow);
-        ArgumentChecks.ensureBetween("numCol", 1, Short.MAX_VALUE, numCol);
+        ArgumentChecks.ensureBetween("numRow", 1, 
Numerics.MAXIMUM_MATRIX_SIZE, numRow);
+        ArgumentChecks.ensureBetween("numCol", 1, 
Numerics.MAXIMUM_MATRIX_SIZE, numCol);
     }
 
     /**
diff --git 
a/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java 
b/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java
index a231706..78b4ca5 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java
@@ -67,6 +67,21 @@ public final class Numerics extends Static {
     }
 
     /**
+     * Maximum number of rows or columns in Apache SIS matrices. We define a 
maximum because SIS is expected to work
+     * mostly with small matrices, because their sizes are related to the 
number of dimensions in coordinate systems.
+     * The maximum should not be greater than {@value Short#MAX_VALUE} in 
order to ensure that {@code rows * columns}
+     * stay below the {@link Integer#MAX_VALUE} / 2 limit.
+     *
+     * <p>We also use this value as a limit for the number of dimensions minus 
1 (we need to subtract one because of
+     * the room needed for the translation column and the extra row in affine 
transforms).  Actually many Apache SIS
+     * classes have their number of dimensions limited mostly by the capacity 
of the {@code int} primitive type, but
+     * we nevertheless set the maximum number of dimensions to a lower value 
for catching probable errors. Note that
+     * this is not a "universal" limit through Apache SIS, as some algorithms 
impose a smaller number of dimensions.
+     * Some limits found in specific Apache SIS code are 20, {@value 
Integer#SIZE} or {@value Long#SIZE}.</p>
+     */
+    public static final int MAXIMUM_MATRIX_SIZE = Short.MAX_VALUE;
+
+    /**
      * Relative difference tolerated when comparing floating point numbers 
using
      * {@link org.apache.sis.util.ComparisonMode#APPROXIMATIVE}.
      *
diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/Store.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/Store.java
index 7604a22..6212aae 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/Store.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/csv/Store.java
@@ -50,13 +50,14 @@ import org.apache.sis.referencing.CRS;
 import org.apache.sis.referencing.CommonCRS;
 import org.apache.sis.internal.referencing.GeodeticObjectBuilder;
 import org.apache.sis.internal.util.UnmodifiableArrayList;
+import org.apache.sis.internal.util.Numerics;
 import org.apache.sis.internal.storage.MetadataBuilder;
 import org.apache.sis.internal.storage.io.IOUtilities;
 import org.apache.sis.internal.storage.io.RewindableLineReader;
-import org.apache.sis.internal.feature.Geometries;
-import org.apache.sis.internal.feature.MovingFeature;
 import org.apache.sis.internal.storage.Resources;
 import org.apache.sis.internal.storage.URIDataStore;
+import org.apache.sis.internal.feature.Geometries;
+import org.apache.sis.internal.feature.MovingFeature;
 import org.apache.sis.geometry.GeneralEnvelope;
 import org.apache.sis.geometry.ImmutableEnvelope;
 import org.apache.sis.metadata.iso.DefaultMetadata;
@@ -436,7 +437,7 @@ final class Store extends URIDataStore implements 
FeatureSet {
                             Errors.Keys.MismatchedDimension_3, 
"@stboundedby(CRS)", spatialDimensionCount, dimension));
                 }
             }
-            if (dimension > Short.MAX_VALUE) {
+            if (dimension >= Numerics.MAXIMUM_MATRIX_SIZE) {
                 throw new DataStoreReferencingException(errors().getString(
                         Errors.Keys.ExcessiveNumberOfDimensions_1, dimension));
             }

Reply via email to