Skip to content

Commit 5543e4e

Browse files
authored
Merge pull request #5920 from radarhere/calloc
Fixed ImagePath.Path array handling
2 parents a8f90a3 + c48271a commit 5543e4e

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

Tests/test_imagepath.py

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def test_path_odd_number_of_coordinates():
9090
[
9191
([0, 1, 2, 3], (0.0, 1.0, 2.0, 3.0)),
9292
([3, 2, 1, 0], (1.0, 0.0, 3.0, 2.0)),
93+
(0, (0.0, 0.0, 0.0, 0.0)),
94+
(1, (0.0, 0.0, 0.0, 0.0)),
9395
],
9496
)
9597
def test_getbbox(coords, expected):

src/path.c

+20-15
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ alloc_array(Py_ssize_t count) {
5757
if ((unsigned long long)count > (SIZE_MAX / (2 * sizeof(double))) - 1) {
5858
return ImagingError_MemoryError();
5959
}
60-
xy = malloc(2 * count * sizeof(double) + 1);
60+
xy = calloc(2 * count * sizeof(double) + 1, sizeof(double));
6161
if (!xy) {
6262
ImagingError_MemoryError();
6363
}
@@ -327,21 +327,26 @@ path_getbbox(PyPathObject *self, PyObject *args) {
327327

328328
xy = self->xy;
329329

330-
x0 = x1 = xy[0];
331-
y0 = y1 = xy[1];
330+
if (self->count == 0) {
331+
x0 = x1 = 0;
332+
y0 = y1 = 0;
333+
} else {
334+
x0 = x1 = xy[0];
335+
y0 = y1 = xy[1];
332336

333-
for (i = 1; i < self->count; i++) {
334-
if (xy[i + i] < x0) {
335-
x0 = xy[i + i];
336-
}
337-
if (xy[i + i] > x1) {
338-
x1 = xy[i + i];
339-
}
340-
if (xy[i + i + 1] < y0) {
341-
y0 = xy[i + i + 1];
342-
}
343-
if (xy[i + i + 1] > y1) {
344-
y1 = xy[i + i + 1];
337+
for (i = 1; i < self->count; i++) {
338+
if (xy[i + i] < x0) {
339+
x0 = xy[i + i];
340+
}
341+
if (xy[i + i] > x1) {
342+
x1 = xy[i + i];
343+
}
344+
if (xy[i + i + 1] < y0) {
345+
y0 = xy[i + i + 1];
346+
}
347+
if (xy[i + i + 1] > y1) {
348+
y1 = xy[i + i + 1];
349+
}
345350
}
346351
}
347352

0 commit comments

Comments
 (0)