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
This commit is contained in:
Nick Burch 2007-01-15 16:40:03 +00:00
parent bf11deb51d
commit 808ac3d6c7
3 changed files with 56 additions and 6 deletions

View File

@ -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);

View File

@ -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);
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);
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());
}
}