Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8262297: ImageIO.write() method will throw IndexOutOfBoundsException #6151

Closed
wants to merge 7 commits into from
Original file line number Diff line number Diff line change
@@ -48,7 +48,6 @@
import java.nio.ByteOrder;
import java.util.Iterator;

import javax.imageio.IIOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
@@ -656,34 +655,30 @@ public void write(IIOMetadata streamMetadata,
}

if (compressionType == BI_RGB || compressionType == BI_BITFIELDS){
try {
switch(dataType) {
case DataBuffer.TYPE_BYTE:
byte[] bdata =
((DataBufferByte)src.getDataBuffer()).getData();
stream.write(bdata, pos, destScanlineLength);
break;

case DataBuffer.TYPE_SHORT:
short[] sdata =
((DataBufferShort)src.getDataBuffer()).getData();
stream.writeShorts(sdata, pos, destScanlineLength);
break;

case DataBuffer.TYPE_USHORT:
short[] usdata =
((DataBufferUShort)src.getDataBuffer()).getData();
stream.writeShorts(usdata, pos, destScanlineLength);
break;

case DataBuffer.TYPE_INT:
int[] idata =
((DataBufferInt)src.getDataBuffer()).getData();
stream.writeInts(idata, pos, destScanlineLength);
break;
}
} catch (IndexOutOfBoundsException e) {
throw new IIOException("Invalid buffer length", e);
switch(dataType) {
case DataBuffer.TYPE_BYTE:
byte[] bdata =
((DataBufferByte)src.getDataBuffer()).getData();
stream.write(bdata, pos, destScanlineLength);
break;

case DataBuffer.TYPE_SHORT:
short[] sdata =
((DataBufferShort)src.getDataBuffer()).getData();
stream.writeShorts(sdata, pos, destScanlineLength);
break;

case DataBuffer.TYPE_USHORT:
short[] usdata =
((DataBufferUShort)src.getDataBuffer()).getData();
stream.writeShorts(usdata, pos, destScanlineLength);
break;

case DataBuffer.TYPE_INT:
int[] idata =
((DataBufferInt)src.getDataBuffer()).getData();
stream.writeInts(idata, pos, destScanlineLength);
break;
}

for(int k=0; k<padding; k++) {
@@ -1461,6 +1456,9 @@ protected boolean canEncodeImage(int compression, ImageTypeSpecifier imgType) {
}
int biType = imgType.getBufferedImageType();
int bpp = imgType.getColorModel().getPixelSize();
if (bpp != 0 && bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks good to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the exception message caused by this should be updated as well? Currently, it takes care of compression only.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think the message should be change. I added bpp information at the end of the exception message.

return false;
}
if (compressionType == BI_RLE4 && bpp != 4) {
// only 4bpp images can be encoded as BI_RLE4
return false;
4 changes: 2 additions & 2 deletions test/jdk/javax/imageio/plugins/bmp/TruncatedPngTest.java
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import javax.imageio.IIOException;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;

@@ -48,7 +48,7 @@ public static void main(String[] args) {
try (InputStream is = new FileInputStream(inputFile)) {
BufferedImage image = ImageIO.read(is);
ImageIO.write(image, "bmp", new File("0.bmp"));
} catch (IIOException e) {
} catch (IOException e) {
System.out.println("Test PASSED.");
passed = true;
} catch (Exception e) {