From f15a4da321fb2fc606be1adb5b95e9f758b24176 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 24 Jan 2022 09:53:19 +0000 Subject: [PATCH] fail image size rescaling of infinite values used git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897406 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/util/ImageUtils.java | 6 +- .../apache/poi/ss/util/TestImageUtils.java | 90 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 poi/src/test/java/org/apache/poi/ss/util/TestImageUtils.java diff --git a/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java b/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java index 45894428dd..b156ba0607 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java +++ b/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java @@ -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 index 0000000000..1487574a49 --- /dev/null +++ b/poi/src/test/java/org/apache/poi/ss/util/TestImageUtils.java @@ -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) + ); + } + } +}