Piotr Karwasz created IMAGING-402:
-------------------------------------

             Summary: PNG, PSD and PCX decoders create the output image without 
checking the Allocator limit
                 Key: IMAGING-402
                 URL: https://issues.apache.org/jira/browse/IMAGING-402
             Project: Commons Imaging
          Issue Type: Bug
          Components: Format: PCX, Format: PNG, Format: PSD
    Affects Versions: 1.0.0-alpha6
            Reporter: Piotr Karwasz


Most decoders check the size of the image they are about to create against the 
{{Allocator}} limit and throw an {{AllocationRequestException}} for dimensions 
beyond it. Three code paths create the output {{BufferedImage}} directly from 
the header dimensions without that check:
* {{PngImageParser.getBufferedImage}}, through 
{{getBufferedImageFactory(params).getColorBufferedImage}} / 
{{getGrayscaleBufferedImage}};
* {{PsdImageParser.getBufferedImage}}, through 
{{getBufferedImageFactory(params).getColorBufferedImage}};
* {{PcxImageParser}}, for 1-bit images with 2 to 4 planes: {{new 
BufferedImage(xSize, ySize, BufferedImage.TYPE_BYTE_BINARY, colorModel)}}.

For PNG and PSD, a small file declaring large dimensions (e.g. 20000 × 20000) 
therefore allocates a 1.6 GB {{int}} raster, or fails with 
{{OutOfMemoryError}}, even when the {{Allocator}} limit is set lower; other 
formats reject the same dimensions with an {{AllocationRequestException}}. For 
PCX the raster is smaller, but the configured limit is still not applied.

Suggested fix: call {{Allocator.check((long) width * height, elementSize)}} 
before each of these allocations, with the element size of the image type 
actually created ({{Integer.BYTES}} for the {{INT}} image types, {{Byte.BYTES}} 
for {{TYPE_BYTE_GRAY}}, one bit per pixel per row for {{TYPE_BYTE_BINARY}}).



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to