Indefinite loop while reading corrupt JPEG
See original GitHub issueHi Harald.
I know reading corrupt files and solving related problems is boring and not very interesting, but since this one hangs the thread it might be worth looking into. Tests have been performed on my code using some Google test suite, and it ends up hanging the application because the thread doing the reading goes into an indefinite loop while holding locks ending up blocking almost everything.
I’ve been able to track it down to an endless loop in com.sun.imageio.plugins.jpeg.JPEGImageReader.hasNextImage()
. The loop is this:
boolean foundFF = false;
for (int byteval = iis.read();
byteval != -1;
byteval = iis.read()) {
if (foundFF == true) {
if (byteval == JPEG.SOI) {
iis.reset();
if (debug) {
System.out.println("true");
}
return true;
}
}
foundFF = (byteval == 0xff) ? true : false;
}
From what I can tell, the only ways to break the loop is either by an Exception
being thrown, -1
being returned from the ImageInputStream
or finding the SOE
. I know that this code isn’t yours, but it seems that the reason for the endless loop is found in com.twelvemonkeys.imageio.plugins.jpeg.JPEGSegmentImageInputStream.repositionAsNecessary()
which swallows the EOFException
. The code comments says that an EOFException
will be thrown or -1
will be returned by the subsequent read, but this clearly isn’t the case.
I’m not familiar enough with this code to understand why the EOFException
is ignored in the first place, and thus not able to produce a fix.
The test suite has thousands of images, and only 8 JPEGs end up in a hung thread, so it’s not a very frequent problem. Still it would be nice to avoid it.
The corrupt/invalid JPEGs are very small (as they have very little actual content), so I’m attaching them all:
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:13 (8 by maintainers)
Top GitHub Comments
Should be good now. 😀
– Harald K
1 year+… Your definition of soon may differ… 😉
Not forgotten though.
– Harald K