From 978c37d699f24d5bdcadf7617e8313433ca0dfdb Mon Sep 17 00:00:00 2001 From: homm Date: Thu, 28 Jul 2016 05:29:24 +0300 Subject: [PATCH] add tests for different PCX encoding cases --- Tests/test_file_pcx.py | 80 +++++++++++++++++++++++++++++++++++++++++- libImaging/PcxEncode.c | 3 -- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/Tests/test_file_pcx.py b/Tests/test_file_pcx.py index db2a526a5..6b15b2fb6 100644 --- a/Tests/test_file_pcx.py +++ b/Tests/test_file_pcx.py @@ -1,6 +1,6 @@ from helper import unittest, PillowTestCase, hopper -from PIL import Image, PcxImagePlugin +from PIL import Image, ImageFile, PcxImagePlugin class TestFilePcx(PillowTestCase): @@ -46,6 +46,84 @@ class TestFilePcx(PillowTestCase): # Make sure all pixels are either 0 or 255. self.assertEqual(im.histogram()[0] + im.histogram()[255], 447*144) + def test_1px_width(self): + im = Image.new('L', (1, 256)) + px = im.load() + for y in range(256): + px[0, y] = y + self._roundtrip(im) + + def test_large_count(self): + im = Image.new('L', (256, 1)) + px = im.load() + for x in range(256): + px[x, 0] = x // 67 * 67 + self._roundtrip(im) + + def _test_overflow(self, im): + _last = ImageFile.MAXBLOCK + ImageFile.MAXBLOCK = 1024 + try: + self._roundtrip(im) + finally: + ImageFile.MAXBLOCK = _last + + def test_break_in_count_overflow(self): + im = Image.new('L', (256, 5)) + px = im.load() + for y in range(4): + for x in range(256): + px[x, y] = x % 128 + self._test_overflow(im) + + def test_break_one_in_loop(self): + im = Image.new('L', (256, 5)) + px = im.load() + for y in range(5): + for x in range(256): + px[x, y] = x % 128 + self._test_overflow(im) + + def test_break_many_in_loop(self): + im = Image.new('L', (256, 5)) + px = im.load() + for y in range(4): + for x in range(256): + px[x, y] = x % 128 + for x in range(8): + px[x, 4] = 16 + self._test_overflow(im) + + def test_break_one_at_end(self): + im = Image.new('L', (256, 5)) + px = im.load() + for y in range(5): + for x in range(256): + px[x, y] = x % 128 + px[0, 3] = 128 + 64 + self._test_overflow(im) + + def test_break_many_at_end(self): + im = Image.new('L', (256, 5)) + px = im.load() + for y in range(5): + for x in range(256): + px[x, y] = x % 128 + for x in range(4): + px[x * 2, 3] = 128 + 64 + px[x + 256 - 4, 3] = 0 + self._test_overflow(im) + + def test_break_padding(self): + im = Image.new('L', (257, 5)) + px = im.load() + for y in range(5): + for x in range(257): + px[x, y] = x % 128 + for x in range(5): + px[x, 3] = 0 + self._test_overflow(im) + if __name__ == '__main__': unittest.main() diff --git a/libImaging/PcxEncode.c b/libImaging/PcxEncode.c index b17a09184..1dd5836e2 100644 --- a/libImaging/PcxEncode.c +++ b/libImaging/PcxEncode.c @@ -168,9 +168,6 @@ ImagingPcxEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) bytes -= 2; } } - if (bytes < padding) { - return ptr - buf; - } /* add the padding */ for (i = 0; i < padding; i++) { ptr[0] = 0;