1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/*
* Copyright 2015 Red Hat, Inc.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8144071
* @run main/othervm MarkTryFinallyReproducer
* @summary Test that call to canDecodeInput in ImageIO don't corrupt
* mark/reset stack in ImageInputStream
* @author Jiri Vanek
*/
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteOrder;
import java.util.Locale;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.IIOByteBuffer;
import javax.imageio.stream.ImageInputStream;
public class MarkTryFinallyReproducer {
private static final byte[] bmp = new byte[]{
127,127, 66, 77, -86, 0, 0, 0, 0, 0, 0, 0,
122, 0, 0, 0, 108, 0, 0, 0, 4, 0, 0, 0, 4,
0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 48, 0, 0,
0, 19, 11, 0, 0, 19, 11, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 66, 71, 82, 115, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -1, -1, -1,
-1, -1, -1, 0, -1, 0, -1, -1, -1, -1, 0, 0, 0, -17,
0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, -1, -1, -1,
-1, 0, 0, 0, -1, -1, -1, -1, -1, -1, 0, 0, -1
};
//first two are evil, we are skipping them later. Others are normal BMP
private static class NotClosingImageInputStream implements ImageInputStream {
private final ImageInputStream src;
private NotClosingImageInputStream(ImageInputStream createImageInputStream) {
this.src = createImageInputStream;
}
@Override
public void setByteOrder(ByteOrder byteOrder) {
src.setByteOrder(byteOrder);
}
@Override
public ByteOrder getByteOrder() {
return src.getByteOrder();
}
@Override
public int read() throws IOException {
return src.read();
}
@Override
public int read(byte[] b) throws IOException {
return src.read(b);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return src.read(b, off, len);
}
@Override
public void readBytes(IIOByteBuffer buf, int len) throws IOException {
src.readBytes(buf, len);
}
@Override
public boolean readBoolean() throws IOException {
return src.readBoolean();
}
@Override
public byte readByte() throws IOException {
return src.readByte();
}
@Override
public int readUnsignedByte() throws IOException {
return src.readUnsignedByte();
}
@Override
public short readShort() throws IOException {
return src.readShort();
}
@Override
public int readUnsignedShort() throws IOException {
return src.readUnsignedShort();
}
@Override
public char readChar() throws IOException {
return src.readChar();
}
@Override
public int readInt() throws IOException {
return src.readInt();
}
@Override
public long readUnsignedInt() throws IOException {
return src.readUnsignedInt();
}
@Override
public long readLong() throws IOException {
return src.readLong();
}
@Override
public float readFloat() throws IOException {
return src.readFloat();
}
@Override
public double readDouble() throws IOException {
return src.readDouble();
}
@Override
public String readLine() throws IOException {
return src.readLine();
}
@Override
public String readUTF() throws IOException {
return src.readUTF();
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException {
src.readFully(b, off, len);
}
@Override
public void readFully(byte[] b) throws IOException {
src.readFully(b);
}
@Override
public void readFully(short[] s, int off, int len) throws IOException {
src.readFully(s, off, len);
}
@Override
public void readFully(char[] c, int off, int len) throws IOException {
src.readFully(c, off, len);
}
@Override
public void readFully(int[] i, int off, int len) throws IOException {
src.readFully(i, off, len);
}
@Override
public void readFully(long[] l, int off, int len) throws IOException {
src.readFully(l, off, len);
}
@Override
public void readFully(float[] f, int off, int len) throws IOException {
src.readFully(f, off, len);
}
@Override
public void readFully(double[] d, int off, int len) throws IOException {
src.readFully(d, off, len);
}
@Override
public long getStreamPosition() throws IOException {
return src.getStreamPosition();
}
@Override
public int getBitOffset() throws IOException {
return src.getBitOffset();
}
@Override
public void setBitOffset(int bitOffset) throws IOException {
src.setBitOffset(bitOffset);
}
@Override
public int readBit() throws IOException {
return src.readBit();
}
@Override
public long readBits(int numBits) throws IOException {
return src.readBits(numBits);
}
@Override
public long length() throws IOException {
return src.length();
}
@Override
public int skipBytes(int n) throws IOException {
return src.skipBytes(n);
}
@Override
public long skipBytes(long n) throws IOException {
return src.skipBytes(n);
}
@Override
public void seek(long pos) throws IOException {
src.seek(pos);
}
@Override
public void mark() {
src.mark();
}
@Override
public void reset() throws IOException {
src.reset();
}
@Override
public void flushBefore(long pos) throws IOException {
src.flushBefore(pos);
}
@Override
public void flush() throws IOException {
src.flush();
}
@Override
public long getFlushedPosition() {
return src.getFlushedPosition();
}
@Override
public boolean isCached() {
return src.isCached();
}
@Override
public boolean isCachedMemory() {
return src.isCachedMemory();
}
@Override
public boolean isCachedFile() {
return src.isCachedFile();
}
@Override
public void close() throws IOException {
//the only important one. nothing
}
}
static final String readerClassName
= MarkTryFinallyReproducerSpi.class.getName();
static final String[] localNames = {"myNames"};
static final String[] localSuffixes = {"mySuffixes"};
static final String[] localMIMETypes = {"myMimes"};
public static class MarkTryFinallyReproducerSpi extends ImageReaderSpi {
public MarkTryFinallyReproducerSpi() {
super("MarkTryFinallyReproducerSpi",
"1.0",
localNames,
localSuffixes,
localMIMETypes,
readerClassName,
new Class[]{ImageInputStream.class},
new String[0],
false,
null,
null,
new String[0],
new String[0],
false,
null,
null,
new String[0],
new String[0]);
}
@Override
public String getDescription(Locale locale) {
return "";
}
@Override
public boolean canDecodeInput(Object input) throws IOException {
throw new IOException("Bad luck");
}
@Override
public ImageReader createReaderInstance(Object extension) {
return null;
}
}
public static void main(String[] args) throws IOException {
MarkTryFinallyReproducerSpi spi = new MarkTryFinallyReproducerSpi();
IIORegistry.getDefaultInstance().registerServiceProvider(spi);
ImageInputStream iis1 =
new NotClosingImageInputStream(ImageIO.createImageInputStream(new ByteArrayInputStream(bmp)));
iis1.readByte();
iis1.mark();
long p1 = iis1.getStreamPosition();
iis1.readByte();
iis1.mark();
long p2 = iis1.getStreamPosition();
BufferedImage bi1 = ImageIO.read(iis1);
iis1.reset();
long pn2 = iis1.getStreamPosition();
iis1.reset();
long pn1 = iis1.getStreamPosition();
if (p1 != pn1 || p2!= pn2) {
throw new RuntimeException("Exception from call to canDecodeInput in ImageIO. " +
"Corrupted stack in ImageInputStream");
}
}
}
|