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

WBMPImageReader doesn't check read() result #75

Open
THausherr opened this issue Apr 30, 2021 · 1 comment
Open

WBMPImageReader doesn't check read() result #75

THausherr opened this issue Apr 30, 2021 · 1 comment

Comments

@THausherr
Copy link

com.sun.imageio.plugins.wbmp.WBMPImageReader doesn't check its read() result:

iis.read(((DataBufferByte)tile.getDataBuffer()).getData(),
                     0, height*sm.getScanlineStride());

It should call readFully() or check the read() result and do a loop. See also
haraldk/TwelveMonkeys#606

Because of that, this code fails if LIMIT is less than the file size:


import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStreamImpl;


public class WBmpBug
{
    static final int LIMIT = 1000; // limit 32000 works
    public static void main(String[] args) throws IOException
    {
        BufferedImage bw = new BufferedImage(500, 500, BufferedImage.TYPE_BYTE_BINARY);
        Graphics2D g = (Graphics2D) bw.getGraphics();
        g.setBackground(Color.white);
        g.fillRect(0, 0, bw.getWidth(), bw.getHeight());
        g.dispose();
        boolean b = ImageIO.write(bw, "wbmp", new File("test2.wbmp"));
        assert b = true;
        BufferedImage bim2 = ImageIO.read(new LimitedImageInputStream(new File("test2.wbmp"), LIMIT));
        //BufferedImage bim2 = ImageIO.read(new File("test2.wbmp")); // this works
        b = ImageIO.write(bim2, "wbmp", new File("test3.wbmp"));
        assert b = true;
        for (int x = 0; x < bim2.getWidth(); ++x)
        {
            for (int y = 0; y < bim2.getHeight(); ++y)
            {
                int i1 = bim2.getRGB(x, y);
                int i2 = bw.getRGB(x, y);
                if (i1 != i2)
                {
                    System.err.println("fail");
                    System.exit(-1);
                }
            }
        }
        System.out.println("success");
    }
    
    static class LimitedImageInputStream extends ImageInputStreamImpl 
    {
        private final RandomAccessFile raf;
        private final int limit;
        
        public LimitedImageInputStream(File file, int limit) throws FileNotFoundException
        {
            raf = new RandomAccessFile(file, "r");
            this.limit = limit;
        }

        @Override
        public int read() throws IOException
        {
            return raf.read();
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException
        {
            int readlen = raf.read(b, off, Math.min(limit, len));
            System.out.println("readLen: " + readlen + " of " + len);
            return readlen;
        }      

        @Override
        public void close() throws IOException
        {
            super.close();
            raf.close();
        }

        @Override
        public void seek(long pos) throws IOException
        {
            super.seek(pos);
            raf.seek(pos);
        }
        
        
    }
}

@THausherr
Copy link
Author

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

1 participant