-
Bug
-
Resolution: Fixed
-
P3
-
8, 9, 11, 13, 14, 15
-
b22
-
generic
-
generic
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8266204 | 11.0.13-oracle | Jayathirth D V | P3 | Resolved | Fixed | b01 |
JDK-8264924 | 11.0.12 | Jayathirth D V | P3 | Resolved | Fixed | b01 |
PNG specification mentions limit of keyword length for different chunks.
But in PNGImageWriter we dont check for keyword length and we just populate the data.
Using below test code i am able to create tEXt chunk with keyword greater than 80.
Test code :
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.IIOImage;
public class MaxLengthKeywordWriteTest {
private static BufferedImage img;
private static ImageWriter writer;
private static ImageWriteParam param;
private static IIOMetadata metadata;
private static void initialize(int type) {
int width = 1;
int height = 1;
img = new BufferedImage(width, height, type);
Graphics2D g2D = img.createGraphics();
g2D.setColor(new Color(255, 255, 255));
g2D.fillRect(0, 0, width, width);
g2D.dispose();
Iterator<ImageWriter> iterWriter =
ImageIO.getImageWritersBySuffix("png");
writer = iterWriter.next();
param = writer.getDefaultWriteParam();
ImageTypeSpecifier specifier =
ImageTypeSpecifier.
createFromBufferedImageType(type);
metadata = writer.getDefaultImageMetadata(specifier, param);
}
private static void createTEXTNode()
throws IIOInvalidTreeException {
IIOMetadataNode tEXt_Entry = new IIOMetadataNode("tEXtEntry");
// Use keyword of length 80
tEXt_Entry.setAttribute("keyword", "Authored" +
"AuthoredAuthoredAuthoredAuthoredAuthoredAuthored" +
"AuthoredAuthoredAuthoredAuthored");
tEXt_Entry.setAttribute("value", "");
IIOMetadataNode tEXt = new IIOMetadataNode("tEXt");
tEXt.appendChild(tEXt_Entry);
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");
root.appendChild(tEXt);
metadata.mergeTree("javax_imageio_png_1.0", root);
}
private static void writeImage() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(metadata, new IIOImage(img, null, metadata), param);
writer.dispose();
baos.close();
}
private static void writePNGTEXTChunk() throws IOException {
initialize(BufferedImage.TYPE_BYTE_GRAY);
// Create tEXt node with text length 0
createTEXTNode();
writeImage();
}
public static void main(String[] args) throws IOException {
// read PNG image where tEXt chunk's text length is 0
writePNGTEXTChunk();
}
}
But in PNGImageWriter we dont check for keyword length and we just populate the data.
Using below test code i am able to create tEXt chunk with keyword greater than 80.
Test code :
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.IIOImage;
public class MaxLengthKeywordWriteTest {
private static BufferedImage img;
private static ImageWriter writer;
private static ImageWriteParam param;
private static IIOMetadata metadata;
private static void initialize(int type) {
int width = 1;
int height = 1;
img = new BufferedImage(width, height, type);
Graphics2D g2D = img.createGraphics();
g2D.setColor(new Color(255, 255, 255));
g2D.fillRect(0, 0, width, width);
g2D.dispose();
Iterator<ImageWriter> iterWriter =
ImageIO.getImageWritersBySuffix("png");
writer = iterWriter.next();
param = writer.getDefaultWriteParam();
ImageTypeSpecifier specifier =
ImageTypeSpecifier.
createFromBufferedImageType(type);
metadata = writer.getDefaultImageMetadata(specifier, param);
}
private static void createTEXTNode()
throws IIOInvalidTreeException {
IIOMetadataNode tEXt_Entry = new IIOMetadataNode("tEXtEntry");
// Use keyword of length 80
tEXt_Entry.setAttribute("keyword", "Authored" +
"AuthoredAuthoredAuthoredAuthoredAuthoredAuthored" +
"AuthoredAuthoredAuthoredAuthored");
tEXt_Entry.setAttribute("value", "");
IIOMetadataNode tEXt = new IIOMetadataNode("tEXt");
tEXt.appendChild(tEXt_Entry);
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");
root.appendChild(tEXt);
metadata.mergeTree("javax_imageio_png_1.0", root);
}
private static void writeImage() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(metadata, new IIOImage(img, null, metadata), param);
writer.dispose();
baos.close();
}
private static void writePNGTEXTChunk() throws IOException {
initialize(BufferedImage.TYPE_BYTE_GRAY);
// Create tEXt node with text length 0
createTEXTNode();
writeImage();
}
public static void main(String[] args) throws IOException {
// read PNG image where tEXt chunk's text length is 0
writePNGTEXTChunk();
}
}
- backported by
-
JDK-8264924 Add length limit for strings in PNGImageWriter
- Resolved
-
JDK-8266204 Add length limit for strings in PNGImageWriter
- Resolved
- relates to
-
JDK-8264923 PNGImageWriter.write_zTXt throws Exception with a typo
- Resolved
-
JDK-8195841 PNGImageReader.readNullTerminatedString() doesnt check for non-null terminated strings with length equal to maxLen
- Resolved