From: Andreas Beeker Date: Mon, 10 Apr 2017 21:43:22 +0000 (+0000) Subject: Fix AIOOBE while extracting hssf pictures which are externally linked X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1f41c31984003d5d3e2bf1f6f8213f5632af399b;p=poi.git Fix AIOOBE while extracting hssf pictures which are externally linked git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1790897 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java index e2d64f96b0..65cb59d1b5 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java @@ -103,6 +103,7 @@ public class HSSFPicture extends HSSFSimpleShape implements Picture { * If the default font is changed the resized image can be streched vertically or horizontally. *

*/ + @Override public void resize(){ resize(Double.MAX_VALUE); } @@ -112,6 +113,7 @@ public class HSSFPicture extends HSSFSimpleShape implements Picture { * * @see #resize(double, double) */ + @Override public void resize(double scale) { resize(scale,scale); } @@ -133,6 +135,7 @@ public class HSSFPicture extends HSSFSimpleShape implements Picture { * @param scaleX the amount by which the image width is multiplied relative to the original width. * @param scaleY the amount by which the image height is multiplied relative to the original height. */ + @Override public void resize(double scaleX, double scaleY) { HSSFClientAnchor anchor = getClientAnchor(); anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE); @@ -157,6 +160,7 @@ public class HSSFPicture extends HSSFSimpleShape implements Picture { * @return HSSFClientAnchor with the preferred size for this image * @since POI 3.0.2 */ + @Override public HSSFClientAnchor getPreferredSize(){ return getPreferredSize(1.0); } @@ -180,6 +184,7 @@ public class HSSFPicture extends HSSFSimpleShape implements Picture { * @return HSSFClientAnchor with the preferred size for this image * @since POI 3.11 */ + @Override public HSSFClientAnchor getPreferredSize(double scaleX, double scaleY){ ImageUtils.setPreferredSize(this, scaleX, scaleY); return getClientAnchor(); @@ -190,6 +195,7 @@ public class HSSFPicture extends HSSFSimpleShape implements Picture { * * @return image dimension in pixels */ + @Override public Dimension getImageDimension(){ InternalWorkbook iwb = getPatriarch().getSheet().getWorkbook().getWorkbook(); EscherBSERecord bse = iwb.getBSERecord(getPictureIndex()); @@ -201,9 +207,15 @@ public class HSSFPicture extends HSSFSimpleShape implements Picture { /** * Return picture data for this shape * - * @return picture data for this shape + * @return picture data for this shape or {@code null} if picture wasn't embedded, i.e. external linked */ + @Override public HSSFPictureData getPictureData(){ + int picIdx = getPictureIndex(); + if (picIdx == -1) { + return null; + } + HSSFPatriarch patriarch = getPatriarch(); HSSFShape parent = getParent(); while(patriarch == null && parent != null) { @@ -215,7 +227,7 @@ public class HSSFPicture extends HSSFSimpleShape implements Picture { } InternalWorkbook iwb = patriarch.getSheet().getWorkbook().getWorkbook(); - EscherBSERecord bse = iwb.getBSERecord(getPictureIndex()); + EscherBSERecord bse = iwb.getBSERecord(picIdx); EscherBlipRecord blipRecord = bse.getBlipRecord(); return new HSSFPictureData(blipRecord); } diff --git a/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java b/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java index 2e74f6c31c..b2822a5094 100644 --- a/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java +++ b/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java @@ -218,7 +218,7 @@ public class EmbeddedExtractor implements Iterable { @Override public boolean canExtract(Picture source) { PictureData pd = source.getPictureData(); - return (pd.getPictureType() == Workbook.PICTURE_TYPE_EMF); + return (pd != null && pd.getPictureType() == Workbook.PICTURE_TYPE_EMF); } /** @@ -232,7 +232,7 @@ public class EmbeddedExtractor implements Iterable { // check for emf+ embedded pdf (poor mans style :( ) // Mac Excel 2011 embeds pdf files with this method. PictureData pd = source.getPictureData(); - if (pd.getPictureType() != Workbook.PICTURE_TYPE_EMF) { + if (pd != null && pd.getPictureType() != Workbook.PICTURE_TYPE_EMF) { return null; } @@ -384,7 +384,9 @@ public class EmbeddedExtractor implements Iterable { int[] failure = computeFailure(pattern); int j = 0; - if (data.length == 0) return -1; + if (data.length == 0) { + return -1; + } for (int i = offset; i < data.length; i++) { while (j > 0 && pattern[j] != data[i]) { diff --git a/test-data/spreadsheet/external_image.xls b/test-data/spreadsheet/external_image.xls new file mode 100644 index 0000000000..d7d2e0e4ea Binary files /dev/null and b/test-data/spreadsheet/external_image.xls differ