question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Indefinite loop while reading corrupt JPEG

See original GitHub issue

Hi 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:

id_005573 sync_jpeg_turbo src_004235 id_000448 src_000000 op_havoc rep_32 cov id_001917 src_001912 op_havoc rep_8 id_001918 src_001917 op_havoc rep_8 id_003682 sync_jpeg_turbo src_001907 id_003723 sync_jpeg_turbo src_001917 id_003809 sync_jpeg_turbo src_001918 id_004235 src_003897 op_havoc rep_16

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
haraldkcommented, May 22, 2018

Should be good now. 😀

– Harald K

2reactions
haraldkcommented, Mar 27, 2018

1 year+… Your definition of soon may differ… 😉

Not forgotten though.

– Harald K

Read more comments on GitHub >

github_iconTop Results From Across the Web

Infinite do-while loop, supposed to be looking for JPEG header
Once again, I'm trying to write a program that copies jpegs from a .raw file. It finds the first header (0xffd8ffe0 or 0xffd8ffe1)...
Read more >
Decoding JPEG puts the process to infinite loop if there is an ...
When you are loading a jpg file with some broken bytes in it, process gets in to infinite loop. Steps to Reproduce. Try...
Read more >
PHP :: Bug #34704 :: Infinite recursion due to corrupt JPEG
Bug #34704, Infinite recursion due to corrupt JPEG ... studied the test image in detail, but my patch allows PHP's Exif functions to...
Read more >
recover.c cannot detect jpeg signatures?
Trying to get my recover.c to work. My program fread's 512 bytes into a buffer on every iteration of a loop. The buffer...
Read more >
Automating the scanning of graphics files for corruption
From the menu select File > Scan and then use the file dialog to browse to the folder in which the images are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found