]> source.dussan.org Git - poi.git/commitdiff
fail image size rescaling of infinite values used
authorPJ Fanning <fanningpj@apache.org>
Mon, 24 Jan 2022 09:53:19 +0000 (09:53 +0000)
committerPJ Fanning <fanningpj@apache.org>
Mon, 24 Jan 2022 09:53:19 +0000 (09:53 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897406 13f79535-47bb-0310-9956-ffa450edef68

poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java
poi/src/test/java/org/apache/poi/ss/util/TestImageUtils.java [new file with mode: 0644]

index 45894428dddffd0452c9992c1d589e3c4cc12184..b156ba060761c1400ec7f036e75fa18e0e9612f8 100644 (file)
@@ -142,8 +142,9 @@ public final class ImageUtils {
      * @param scaleX the amount by which image width is multiplied relative to the original width.
      * @param scaleY the amount by which image height is multiplied relative to the original height.
      * @return the new Dimensions of the scaled picture in EMUs
+     * @throws IllegalArgumentException if scale values lead to negative or infinite results
      */
-    public static Dimension setPreferredSize(Picture picture, double scaleX, double scaleY){
+    public static Dimension setPreferredSize(Picture picture, double scaleX, double scaleY) {
         ClientAnchor anchor = picture.getClientAnchor();
         boolean isHSSF = (anchor instanceof HSSFClientAnchor);
         PictureData data = picture.getPictureData();
@@ -220,6 +221,9 @@ public final class ImageUtils {
         if (targetSize < 0) {
             throw new IllegalArgumentException("target size < 0");
         }
+        if (Double.isInfinite(targetSize) || Double.isNaN(targetSize)) {
+            throw new IllegalArgumentException("target size " + targetSize + " is not supported");
+        }
 
         int cellIdx = startCell;
         double dim, delta;
diff --git a/poi/src/test/java/org/apache/poi/ss/util/TestImageUtils.java b/poi/src/test/java/org/apache/poi/ss/util/TestImageUtils.java
new file mode 100644 (file)
index 0000000..1487574
--- /dev/null
@@ -0,0 +1,90 @@
+/* ====================================================================
+   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.poi.ss.util;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
+import org.apache.poi.hssf.usermodel.HSSFPatriarch;
+import org.apache.poi.hssf.usermodel.HSSFPicture;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Tests ImageUtils.
+ *
+ * @see ImageUtils
+ */
+final class TestImageUtils {
+
+    @Test
+    void testSetPreferredSizeNegativeScale() throws IOException {
+        try (HSSFWorkbook wb = new HSSFWorkbook()) {
+            HSSFSheet sheet = wb.createSheet();
+            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
+
+            byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("45829.png");
+            int idx1 = wb.addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);
+            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 10, 10, (short)0, 0, (short)10, 10);
+            HSSFPicture picture = patriarch.createPicture(anchor, idx1);
+
+            assertThrows(IllegalArgumentException.class, () ->
+                    ImageUtils.setPreferredSize(picture, -1, 1)
+            );
+            assertThrows(IllegalArgumentException.class, () ->
+                    ImageUtils.setPreferredSize(picture, 1, -1)
+            );
+        }
+    }
+
+    @Test
+    void testSetPreferredSizeInfiniteScale() throws IOException {
+        try (HSSFWorkbook wb = new HSSFWorkbook()) {
+            HSSFSheet sheet = wb.createSheet();
+            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
+
+            byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("45829.png");
+            int idx1 = wb.addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);
+            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 10, 10, (short)0, 0, (short)10, 10);
+            HSSFPicture picture = patriarch.createPicture(anchor, idx1);
+
+            assertThrows(IllegalArgumentException.class, () ->
+                    ImageUtils.setPreferredSize(picture, Double.POSITIVE_INFINITY, 1)
+            );
+            assertThrows(IllegalArgumentException.class, () ->
+                    ImageUtils.setPreferredSize(picture, 1, Double.NEGATIVE_INFINITY)
+            );
+            assertThrows(IllegalArgumentException.class, () ->
+                    ImageUtils.setPreferredSize(picture, 1, Double.POSITIVE_INFINITY)
+            );
+            assertThrows(IllegalArgumentException.class, () ->
+                    ImageUtils.setPreferredSize(picture, Double.NEGATIVE_INFINITY, 1)
+            );
+            assertThrows(IllegalArgumentException.class, () ->
+                    ImageUtils.setPreferredSize(picture, Double.NaN, 1)
+            );
+            assertThrows(IllegalArgumentException.class, () ->
+                    ImageUtils.setPreferredSize(picture, 1, Double.NaN)
+            );
+        }
+    }
+}