Skip to content

Commit

Permalink
Merge 0c4131b into 34c80ef
Browse files Browse the repository at this point in the history
  • Loading branch information
wiredfool committed Mar 26, 2016
2 parents 34c80ef + 0c4131b commit 07786a5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
38 changes: 36 additions & 2 deletions Tests/test_imagepath.py
@@ -1,9 +1,9 @@
from helper import unittest, PillowTestCase

from PIL import ImagePath
from PIL import ImagePath, Image

import array

import struct

class TestImagePath(PillowTestCase):

Expand Down Expand Up @@ -62,6 +62,40 @@ def test_path(self):
self.assertEqual(list(p), [(0.0, 1.0)])


def test_overflow_segfault(self):
try:
# post patch, this fails with a memory error
x = evil()

# This fails due to the invalid malloc above,
# and segfaults
for i in range(200000):
if str is bytes:
x[i] = "0"*16
else:
x[i] = b'0'*16
except TypeError as msg:
# Some pythons fail getting the argument as an integer, and
# it falls through to the sequence. Seeing this on 32bit windows.
self.assertTrue(True, "Sequence required")
except MemoryError as msg:
self.assertTrue(msg)
except:
self.assertTrue(False, "Should have received a memory error")


class evil:
def __init__(self):
self.corrupt = Image.core.path(0x4000000000000000)

def __getitem__(self, i):
x = self.corrupt[i]
return struct.pack("dd", x[0], x[1])

def __setitem__(self, i, x):
self.corrupt[i] = struct.unpack("dd", x)


if __name__ == '__main__':
unittest.main()

Expand Down
4 changes: 4 additions & 0 deletions path.c
Expand Up @@ -57,6 +57,10 @@ alloc_array(Py_ssize_t count)
PyErr_NoMemory();
return NULL;
}
if (count > (SIZE_MAX / (2 * sizeof(double))) - 1 ) {
PyErr_NoMemory();
return NULL;
}
xy = malloc(2 * count * sizeof(double) + 1);
if (!xy)
PyErr_NoMemory();
Expand Down

0 comments on commit 07786a5

Please sign in to comment.