coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.14k stars 784 forks source link

java.lang.ArithmeticException: / by zero, probably only for -Dthumbnailator.conserveMemoryWorkaround=true #161

Closed dmitry-weirdo closed 3 years ago

dmitry-weirdo commented 3 years ago

Expected behavior

Thumbnail is generated without any exceptions.

Actual behavior

Lots of jpeg files (not all!) fail with the following exceptions:

java.lang.ArithmeticException: / by zero
        at net.coobird.thumbnailator.tasks.io.InputStreamImageSource.readImage(Unknown Source) ~[thumbnailator-0.4.12.jar:0.4.12]
        at net.coobird.thumbnailator.tasks.io.InputStreamImageSource.read(Unknown Source) ~[thumbnailator-0.4.12.jar:0.4.12]
        at net.coobird.thumbnailator.tasks.io.FileImageSource.read(Unknown Source) ~[thumbnailator-0.4.12.jar:0.4.12]
        at net.coobird.thumbnailator.tasks.SourceSinkThumbnailTask.read(Unknown Source) ~[thumbnailator-0.4.12.jar:0.4.12]
        at net.coobird.thumbnailator.Thumbnailator.createThumbnail(Unknown Source) ~[thumbnailator-0.4.12.jar:0.4.12]
        at net.coobird.thumbnailator.Thumbnails$Builder.toFile(Unknown Source) ~[thumbnailator-0.4.12.jar:0.4.12]

Steps to reproduce the behavior

I ran into constant OOM on big files (namely the 6000*4000 jpeg), so I ran the import with -Dthumbnailator.conserveMemoryWorkaround=true. This prevents OOM, but big percentage of files fail with the aforementioned ArithmeticException. Most probably these are big files that will otherwise fail with OOM.

The thumbnail generation code is:

    public static void generateThumbnail(File imageFile, File thumbnailFile, Integer width) {
        try {
            // Make thumbnail
            Thumbnails
                .of(imageFile)
                .width(width)
                .outputFormat("jpg")
                .toFile(thumbnailFile);

            logger.debug("Thumbnail of image file {} generated to file {}", imageFile.getPath(), thumbnailFile.getPath());
        }
        catch (IOException e) {
            logger.error(String.format("Error while generating thumbnail of file %s to thumbnail file %s", imageFile.getPath(), thumbnailFile.getPath()), e);

            throw new RuntimeException(e);
        }
    }

Environment

dmitry-weirdo commented 3 years ago

WP_20170807_20_47_27_Pro

This is an example image on which the exception occurs.

coobird commented 3 years ago

@dmitry-weirdo, thank you for reporting this.

There's definitely some division happening, and I may have overlooked some possible division by zero situations. I can't address this immediately, but I'll try to follow-up on this as soon as I can.

dmitry-weirdo commented 3 years ago

@coobird Any progress on this issue?

coobird commented 3 years ago

@dmitry-weirdo, I was hoping to take a look at it this weekend. I'll try to take a quick look at it now as well :)

coobird commented 3 years ago

Reproduced issue; happens when .width or .height is called.

Hope to have a fix out this weekend.

coobird commented 3 years ago

Fixed in 0.4.13.