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/08/01 12:14:42 UTC
[sis] 03/03: Ensure that the chunk size are not greater than buffer
capacity.
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 65807794bed50185192a57480deed421e5bb0a6c
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Sat Jul 31 21:45:45 2021 +0200
Ensure that the chunk size are not greater than buffer capacity.
---
.../apache/sis/internal/geotiff/CopyFromBytes.java | 2 +-
.../org/apache/sis/internal/geotiff/Inflater.java | 32 +++++++++--
.../apache/sis/internal/geotiff/InflaterTest.java | 64 ++++++++++++++++++++++
.../apache/sis/test/suite/GeoTiffTestSuite.java | 1 +
4 files changed, 92 insertions(+), 7 deletions(-)
diff --git a/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/CopyFromBytes.java b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/CopyFromBytes.java
index 4fe7a12..6287cb1 100644
--- a/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/CopyFromBytes.java
+++ b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/CopyFromBytes.java
@@ -65,7 +65,7 @@ abstract class CopyFromBytes extends Inflater {
private CopyFromBytes(final ChannelDataInput input, final long start, final int elementsPerRow,
final int samplesPerElement, final int[] skipAfterElements, final int sampleSize)
{
- super(input, elementsPerRow, samplesPerElement, skipAfterElements);
+ super(input, elementsPerRow, samplesPerElement, skipAfterElements, input.buffer.capacity() / sampleSize);
this.streamPosition = start;
this.sampleSize = sampleSize;
}
diff --git a/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Inflater.java b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Inflater.java
index 6848964..1f9d588 100644
--- a/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Inflater.java
+++ b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Inflater.java
@@ -18,6 +18,9 @@ package org.apache.sis.internal.geotiff;
import java.nio.Buffer;
import java.io.IOException;
+import java.util.Arrays;
+import org.apache.sis.math.MathFunctions;
+import org.apache.sis.internal.util.Numerics;
import org.apache.sis.internal.storage.io.ChannelDataInput;
import static org.apache.sis.util.ArgumentChecks.ensureStrictlyPositive;
@@ -72,11 +75,11 @@ public abstract class Inflater {
* @param elementsPerRow number of elements (usually pixels) per row. Must be strictly positive.
* @param samplesPerElement number of sample values per element (usually pixel). Must be strictly positive.
* @param skipAfterElements number of sample values to skip between elements (pixels). May be empty or null.
+ * @param maxChunkSize maximal value (in number of elements) for the {@link #samplesPerChunk} field.
*/
- protected Inflater(final ChannelDataInput input, final int elementsPerRow, final int samplesPerElement, final int[] skipAfterElements) {
- ensureNonNull("input", input);
- ensureStrictlyPositive("elementsPerRow", elementsPerRow);
- ensureStrictlyPositive("samplesPerElement", samplesPerElement);
+ protected Inflater(final ChannelDataInput input, final int elementsPerRow, final int samplesPerElement,
+ final int[] skipAfterElements, final int maxChunkSize)
+ {
this.input = input;
skipAfterChunks = skipAfterElements;
if (skipAfterElements != null) {
@@ -86,8 +89,22 @@ public abstract class Inflater {
chunksPerRow = elementsPerRow;
samplesPerChunk = samplesPerElement;
} else {
- chunksPerRow = 1;
- samplesPerChunk = Math.multiplyExact(samplesPerElement, elementsPerRow);
+ int n = 1;
+ int s = Math.multiplyExact(samplesPerElement, elementsPerRow);
+ if (s > maxChunkSize) {
+ /*
+ * We want the smallest divisor n ≥ s/maxChunkSize. Note that `i` is guaranteed
+ * to be inside the array index range because the last array element is `s` and
+ * the value that we search can not be greater.
+ */
+ final int[] divisors = MathFunctions.divisors(s);
+ int i = Arrays.binarySearch(divisors, Numerics.ceilDiv(s, maxChunkSize));
+ if (i < 0) i = ~i; // No need for array bound check.
+ n = divisors[i];
+ s /= n;
+ }
+ chunksPerRow = n;
+ samplesPerChunk = s;
}
}
@@ -107,6 +124,9 @@ public abstract class Inflater {
public static Inflater create(final Compression compression, final ChannelDataInput input, final long start,
final int elementsPerRow, final int samplesPerElement, final int[] skipAfterElements, final Buffer target)
{
+ ensureNonNull("input", input);
+ ensureStrictlyPositive("elementsPerRow", elementsPerRow);
+ ensureStrictlyPositive("samplesPerElement", samplesPerElement);
switch (compression) {
case NONE: {
return CopyFromBytes.create(input, start, elementsPerRow, samplesPerElement, skipAfterElements, target);
diff --git a/storage/sis-geotiff/src/test/java/org/apache/sis/internal/geotiff/InflaterTest.java b/storage/sis-geotiff/src/test/java/org/apache/sis/internal/geotiff/InflaterTest.java
new file mode 100644
index 0000000..0df80ca
--- /dev/null
+++ b/storage/sis-geotiff/src/test/java/org/apache/sis/internal/geotiff/InflaterTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.geotiff;
+
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests the {@link Inflater} class.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @version 1.1
+ * @since 0.8
+ * @module
+ */
+public final strictfp class InflaterTest extends TestCase {
+ /**
+ * Verifies that the inflater constructor makes sure to have a chunk size
+ * not greater than maximal buffer capacity.
+ */
+ @Test
+ public void testChunkSizeLimit() {
+ // With chunk size below size limit.
+ Mock inflater = new Mock(131, 5, null, 1000);
+ assertEquals( 1, inflater.chunksPerRow);
+ assertEquals(655, inflater.samplesPerChunk);
+ assertNull ( inflater.skipAfterChunks);
+
+ // With chunk size above size limit.
+ inflater = new Mock(131, 5, null, 400);
+ assertEquals( 5, inflater.chunksPerRow);
+ assertEquals(131, inflater.samplesPerChunk);
+ assertNull ( inflater.skipAfterChunks);
+ }
+
+ /**
+ * A dummy inflater implementation doing nothing.
+ */
+ private static final class Mock extends Inflater {
+ Mock(int elementsPerRow, int samplesPerElement, int[] skipAfterElements, int maxChunkSize) {
+ super(null, elementsPerRow, samplesPerElement, skipAfterElements, maxChunkSize);
+ }
+
+ @Override public void uncompressRow() {}
+ @Override public void skip(long n) {}
+ }
+}
diff --git a/storage/sis-geotiff/src/test/java/org/apache/sis/test/suite/GeoTiffTestSuite.java b/storage/sis-geotiff/src/test/java/org/apache/sis/test/suite/GeoTiffTestSuite.java
index 2b92a07..9bc5e14 100644
--- a/storage/sis-geotiff/src/test/java/org/apache/sis/test/suite/GeoTiffTestSuite.java
+++ b/storage/sis-geotiff/src/test/java/org/apache/sis/test/suite/GeoTiffTestSuite.java
@@ -31,6 +31,7 @@ import org.junit.BeforeClass;
*/
@Suite.SuiteClasses({
org.apache.sis.internal.geotiff.CompressionTest.class,
+ org.apache.sis.internal.geotiff.InflaterTest.class,
org.apache.sis.storage.geotiff.TypeTest.class,
org.apache.sis.storage.geotiff.GeoKeysTest.class,
org.apache.sis.storage.geotiff.CRSBuilderTest.class,