From: Nick Burch Date: Mon, 15 Jan 2007 16:40:03 +0000 (+0000) Subject: Fix for bug #41357, by moving byte array creation until after we've decided that... X-Git-Tag: REL_3_0_RC1~33 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=808ac3d6c7b64d23dabc0c78917a798020a6721e;p=poi.git Fix for bug #41357, by moving byte array creation until after we've decided that we have a valid picture git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@496398 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java b/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java index 774129a9cc..513d42ab50 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java +++ b/src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java @@ -270,8 +270,6 @@ public class HSLFSlideShow extends POIDocument // Image size (excluding the 8 byte header) int imgsize = LittleEndian.getInt(pictstream, pos); pos += LittleEndian.INT_SIZE; - byte[] imgdata = new byte[imgsize]; - System.arraycopy(pictstream, pos, imgdata, 0, imgdata.length); // The image size must be 0 or greater // (0 is allowed, but odd, since we do wind on by the header each @@ -282,8 +280,15 @@ public class HSLFSlideShow extends POIDocument // If they type (including the bonus 0xF018) is 0, skip it if(type == 0) { - System.err.println("Problem reading picture: Invalid image type 0, on picture with length" + imgsize + ".\nYou document will probably become corrupted if you save it!"); + System.err.println("Problem reading picture: Invalid image type 0, on picture with length " + imgsize + ".\nYou document will probably become corrupted if you save it!"); + System.err.println(pos); } else { + // Copy the data, ready to pass to PictureData + byte[] imgdata = new byte[imgsize]; + if(imgsize > 0) { + System.arraycopy(pictstream, pos, imgdata, 0, imgdata.length); + } + // Build the PictureData object from the data try { PictureData pict = PictureData.create(type - 0xF018); diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/data/PictureLengthZero.ppt b/src/scratchpad/testcases/org/apache/poi/hslf/data/PictureLengthZero.ppt new file mode 100644 index 0000000000..1cb0cda0fa Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hslf/data/PictureLengthZero.ppt differ diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java index f78e9df824..797f97cda7 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java @@ -387,9 +387,54 @@ public class TestPictures extends TestCase{ assertEquals(Picture.WMF, hslf.getPictures()[0].getType()); assertEquals(Picture.WMF, hslf.getPictures()[1].getType()); - // TODO: DISABLED: Pending bug #41176 + // Now test what happens when we use the SlideShow interface + SlideShow ppt = new SlideShow(hslf); + Slide[] slides = ppt.getSlides(); + PictureData[] pictures = ppt.getPictureData(); + assertEquals(12, slides.length); + assertEquals(2, pictures.length); + + Picture pict; + PictureData pdata; + + pict = (Picture)slides[0].getShapes()[1]; // 2nd object on 1st slide + pdata = pict.getPictureData(); + assertTrue(pdata instanceof WMF); + assertEquals(Picture.WMF, pdata.getType()); + + pict = (Picture)slides[0].getShapes()[2]; // 3rd object on 1st slide + pdata = pict.getPictureData(); + assertTrue(pdata instanceof WMF); + assertEquals(Picture.WMF, pdata.getType()); + } + + public void testZeroPictureLength() throws Exception { + HSLFSlideShow hslf = new HSLFSlideShow(new File(cwd, "PictureLengthZero.ppt").getPath()); + // Should still have 2 real pictures + assertEquals(2, hslf.getPictures().length); + // Both are real pictures, both WMF + assertEquals(Picture.WMF, hslf.getPictures()[0].getType()); + assertEquals(Picture.WMF, hslf.getPictures()[1].getType()); + // Now test what happens when we use the SlideShow interface - //SlideShow ppt = new SlideShow(hslf); + SlideShow ppt = new SlideShow(hslf); + Slide[] slides = ppt.getSlides(); + PictureData[] pictures = ppt.getPictureData(); + assertEquals(27, slides.length); + assertEquals(2, pictures.length); + + Picture pict; + PictureData pdata; + + pict = (Picture)slides[6].getShapes()[13]; + pdata = pict.getPictureData(); + assertTrue(pdata instanceof WMF); + assertEquals(Picture.WMF, pdata.getType()); + + pict = (Picture)slides[7].getShapes()[13]; + pdata = pict.getPictureData(); + assertTrue(pdata instanceof WMF); + assertEquals(Picture.WMF, pdata.getType()); } }