diff --git a/src/net/sf/image4j/codec/ico/ICODecoder.java b/src/net/sf/image4j/codec/ico/ICODecoder.java index fe1a29d..700bb6d 100644 --- a/src/net/sf/image4j/codec/ico/ICODecoder.java +++ b/src/net/sf/image4j/codec/ico/ICODecoder.java @@ -167,6 +167,9 @@ public static java.util.List readExt(java.io.InputStream is) try { for (i = 0; i < sCount; i++) { // Make sure we're at the right file offset! + while (in.getCount() < entries[i].iFileOffset) { + in.skip(entries[i].iFileOffset - in.getCount(), false); + } int fileOffset = in.getCount(); if (fileOffset != entries[i].iFileOffset) { throw new IOException("Cannot read image #" + i diff --git a/src/net/sf/image4j/io/LittleEndianInputStream.java b/src/net/sf/image4j/io/LittleEndianInputStream.java index 70f5839..d1630c2 100644 --- a/src/net/sf/image4j/io/LittleEndianInputStream.java +++ b/src/net/sf/image4j/io/LittleEndianInputStream.java @@ -31,8 +31,13 @@ public int getCount() { return ((CountingInputStream) in).getCount(); } - public int skip(int count, boolean strict) throws IOException { - return IOUtils.skip(this, count, strict); + @Override + public long skip( long n ) throws IOException { + return skip( ( int )n, false ); + } + + public int skip( int count, boolean strict ) throws IOException { + return IOUtils.skip( this, count, strict ); } /** diff --git a/test/net/sf/image4j/254.ico b/test/net/sf/image4j/254.ico new file mode 100644 index 0000000..18fa310 Binary files /dev/null and b/test/net/sf/image4j/254.ico differ diff --git a/test/net/sf/image4j/ICODecoderTest.java b/test/net/sf/image4j/ICODecoderTest.java new file mode 100644 index 0000000..98be10e --- /dev/null +++ b/test/net/sf/image4j/ICODecoderTest.java @@ -0,0 +1,20 @@ +package net.sf.image4j; + +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Test; + +import net.sf.image4j.codec.ico.ICODecoder; + +public class ICODecoderTest { + + @Test + public void checkSkipOnTooLongLayerDefinition() throws IOException { + // The image header is some bytes after the last layer definition, so the offset is higher than the current position + try(InputStream icon = ICODecoderTest.class.getResourceAsStream("254.ico")){ + ICODecoder.read(icon); + } + } + +}