Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite Loop Reading Image #7

Open
afboteroz opened this issue Sep 20, 2016 · 7 comments
Open

Infinite Loop Reading Image #7

afboteroz opened this issue Sep 20, 2016 · 7 comments

Comments

@afboteroz
Copy link

afboteroz commented Sep 20, 2016

I'm using jpedal to get an image of a pdf and where jpedal tries to read the imagen it's enter in an infinite
loop.

https://github.com/jai-imageio/jai-imageio-jpeg2000/blob/master/src/main/java/jj2000/j2k/codestream/reader/PktDecoder.java#L1028

while(bin.readBit()!=0) {
    lblock[c][r][s][cbc.y][cbc.x]++;
}

Execution Code:

byte[] data;

...

try {
    in = new ByteArrayInputStream(data);
    iir = ImageIO.getImageReadersByFormatName("JPEG2000").next();
} catch (Exception ee) {
    ...
}
...
try{
    ImageInputStream iin = ImageIO.createImageInputStream(in);
    try{
        iir.setInput(iin, true);  
        image = iir.read(0);
        ...
    } catch (Exception ee) {
    ...
    }
} catch (Exception ee) {
    ...
}
@stain
Copy link
Member

stain commented Feb 13, 2017

Do you have a copy of the data that causes an infinite loop? I can't see how readBit() would cause an infinite loop as it should progress through the stream until bpos reaches 0?

Or is the problem with bit stuffing..? It assumes nextbbuf has been updated, but if you have a stream with more than 7 0xFFs I guess it could fall over and just keep looking within the same bytes.

@afboteroz
Copy link
Author

The PDF that was trying to extract the first image was this: 00006567119070520-1-original.pdf

@clockard
Copy link

I have been experiencing this issue as well.

@clockard
Copy link

I have pr up to prevent the infinite loop from happening on the codice fork of the project here codice#3

@clockard
Copy link

clockard commented Feb 16, 2018

This is the j2k file that was causing the issue for me. I gave it a pdf extension so github would let me upload it.
p1_05.pdf

@Lonzak
Copy link

Lonzak commented Dec 11, 2020

Seems to have been merged, so this can be closed now?

@igorrogov
Copy link

I have a PDF file where I can see a similar issue. I use PDFBox to extract an embedded image in JPEG2000 format, but it gets stuck in infinite loop in:

        at jj2000.j2k.codestream.reader.PktDecoder.readPktHead(PktDecoder.java:1027)
        at jj2000.j2k.codestream.reader.FileBitstreamReaderAgent.readResLyCompPos(FileBitstreamReaderAgent.java:1162)
        at jj2000.j2k.codestream.reader.FileBitstreamReaderAgent.readTilePkts(FileBitstreamReaderAgent.java:1963)
        at jj2000.j2k.codestream.reader.FileBitstreamReaderAgent.setTile(FileBitstreamReaderAgent.java:2228)
        at jj2000.j2k.wavelet.synthesis.MultiResImgDataAdapter.setTile(MultiResImgDataAdapter.java:291)
        at jj2000.j2k.wavelet.synthesis.MultiResImgDataAdapter.setTile(MultiResImgDataAdapter.java:291)
        at jj2000.j2k.quantization.dequantizer.Dequantizer.setTile(Dequantizer.java:226)
        at jj2000.j2k.wavelet.synthesis.InvWTAdapter.setTile(InvWTAdapter.java:339)
        at jj2000.j2k.wavelet.synthesis.InvWTFull.setTile(InvWTFull.java:578)
        at jj2000.j2k.image.ImgDataAdapter.setTile(ImgDataAdapter.java:288)
        at jj2000.j2k.image.invcomptransf.InvCompTransf.setTile(InvCompTransf.java:643)
        at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.readSubsampledRaster(J2KReadState.java:763)
        at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.readBufferedImage(J2KReadState.java:384)
        at com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader.read(J2KImageReader.java:454)
        at org.apache.pdfbox.filter.JPXFilter.readJPX(JPXFilter.java:132)
        at org.apache.pdfbox.filter.JPXFilter.decode(JPXFilter.java:64)
        at org.apache.pdfbox.cos.COSInputStream.create(COSInputStream.java:80)
        at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:176)
        at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:164)
        at org.apache.pdfbox.pdmodel.common.PDStream.createInputStream(PDStream.java:236)
        at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.<init>(PDImageXObject.java:142)
        at org.apache.pdfbox.pdmodel.graphics.PDXObject.createXObject(PDXObject.java:70)
        at org.apache.pdfbox.pdmodel.PDResources.getXObject(PDResources.java:426)
        at org.apache.pdfbox.contentstream.operator.graphics.DrawObject.process(DrawObject.java:58)
        at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:933)
        at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:515)
        at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:489)
        at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:156)
        at org.apache.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:275)
        at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:458)
        at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:414)
        at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:396)
        at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:379)

I have tried the patch from the other fork where it's supposed to be fixed (https://github.com/codice/jai-imageio-jpeg2000), but it didn't work for me. The PDF looks fine when I look at it in Adobe.

Sample file is attached:
The digital overhaul Exec Report.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants