From 1e092419b6806495c683043ab3feb6ce264f3b9c Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 6 Dec 2021 22:24:19 +1100 Subject: [PATCH 1/2] Initialize coordinates to zero --- Tests/test_imagepath.py | 1 + src/path.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/test_imagepath.py b/Tests/test_imagepath.py index 0835fdb4361..cd850bb1c78 100644 --- a/Tests/test_imagepath.py +++ b/Tests/test_imagepath.py @@ -90,6 +90,7 @@ def test_path_odd_number_of_coordinates(): [ ([0, 1, 2, 3], (0.0, 1.0, 2.0, 3.0)), ([3, 2, 1, 0], (1.0, 0.0, 3.0, 2.0)), + (1, (0.0, 0.0, 0.0, 0.0)), ], ) def test_getbbox(coords, expected): diff --git a/src/path.c b/src/path.c index 4764c58aa04..64c767cb88d 100644 --- a/src/path.c +++ b/src/path.c @@ -57,7 +57,7 @@ alloc_array(Py_ssize_t count) { if ((unsigned long long)count > (SIZE_MAX / (2 * sizeof(double))) - 1) { return ImagingError_MemoryError(); } - xy = malloc(2 * count * sizeof(double) + 1); + xy = calloc(2 * count * sizeof(double) + 1, sizeof(double)); if (!xy) { ImagingError_MemoryError(); } From c48271ab354db49cdbd740bc45e13be4f0f7993c Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 6 Dec 2021 22:25:14 +1100 Subject: [PATCH 2/2] Handle case where path count is zero --- Tests/test_imagepath.py | 1 + src/path.c | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Tests/test_imagepath.py b/Tests/test_imagepath.py index cd850bb1c78..b18271cc5a1 100644 --- a/Tests/test_imagepath.py +++ b/Tests/test_imagepath.py @@ -90,6 +90,7 @@ def test_path_odd_number_of_coordinates(): [ ([0, 1, 2, 3], (0.0, 1.0, 2.0, 3.0)), ([3, 2, 1, 0], (1.0, 0.0, 3.0, 2.0)), + (0, (0.0, 0.0, 0.0, 0.0)), (1, (0.0, 0.0, 0.0, 0.0)), ], ) diff --git a/src/path.c b/src/path.c index 64c767cb88d..dea274ee336 100644 --- a/src/path.c +++ b/src/path.c @@ -327,21 +327,26 @@ path_getbbox(PyPathObject *self, PyObject *args) { xy = self->xy; - x0 = x1 = xy[0]; - y0 = y1 = xy[1]; + if (self->count == 0) { + x0 = x1 = 0; + y0 = y1 = 0; + } else { + x0 = x1 = xy[0]; + y0 = y1 = xy[1]; - for (i = 1; i < self->count; i++) { - if (xy[i + i] < x0) { - x0 = xy[i + i]; - } - if (xy[i + i] > x1) { - x1 = xy[i + i]; - } - if (xy[i + i + 1] < y0) { - y0 = xy[i + i + 1]; - } - if (xy[i + i + 1] > y1) { - y1 = xy[i + i + 1]; + for (i = 1; i < self->count; i++) { + if (xy[i + i] < x0) { + x0 = xy[i + i]; + } + if (xy[i + i] > x1) { + x1 = xy[i + i]; + } + if (xy[i + i + 1] < y0) { + y0 = xy[i + i + 1]; + } + if (xy[i + i + 1] > y1) { + y1 = xy[i + i + 1]; + } } }