Skip to content

Commit f260acc

Browse files
authored
Merge pull request #4506 from hugovk/fix_pcx
Fix bounds overflow in PCX decoding
2 parents 9650ac4 + ada137e commit f260acc

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

Tests/images/01r_00.pcx

836 Bytes
Binary file not shown.

Tests/test_image.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,17 @@ def test_pillow_version(self, test_module):
638638
assert test_module.PILLOW_VERSION > "7.0.0"
639639

640640
def test_overrun(self):
641+
""" For overrun completeness, test as:
642+
valgrind pytest -qq Tests/test_image.py::TestImage::test_overrun | grep decode.c
643+
"""
641644
for file in [
642645
"fli_overrun.bin",
643646
"sgi_overrun.bin",
644647
"sgi_overrun_expandrow.bin",
645648
"sgi_overrun_expandrow2.bin",
646649
"pcx_overrun.bin",
647650
"pcx_overrun2.bin",
651+
"01r_00.pcx",
648652
]:
649653
with Image.open(os.path.join("Tests/images", file)) as im:
650654
try:

src/libImaging/PcxDecode.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
2222
UINT8 n;
2323
UINT8* ptr;
2424

25-
if (strcmp(im->mode, "1") == 0 && state->xsize > state->bytes * 8) {
26-
state->errcode = IMAGING_CODEC_OVERRUN;
27-
return -1;
28-
} else if (strcmp(im->mode, "P") == 0 && state->xsize > state->bytes) {
25+
if ((state->xsize * state->bits + 7) / 8 > state->bytes) {
2926
state->errcode = IMAGING_CODEC_OVERRUN;
3027
return -1;
3128
}

0 commit comments

Comments
 (0)