Skip to content

Commit a99361a

Browse files
committed
Raise an error if path is compacted during mapping
1 parent 7e6e08e commit a99361a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Tests/test_imagepath.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ def test_overflow_segfault() -> None:
204204
x[i] = b"0" * 16
205205

206206

207+
def test_compact_within_map() -> None:
208+
p = ImagePath.Path([0, 1])
209+
210+
def map_func(x: float, y: float) -> tuple[float, float]:
211+
p.compact()
212+
return 0, 0
213+
214+
with pytest.raises(ValueError):
215+
p.map(map_func)
216+
217+
207218
class Evil:
208219
def __init__(self) -> None:
209220
self.corrupt = Image.core.path(0x4000000000000000)

src/path.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view);
4444
typedef struct {
4545
PyObject_HEAD Py_ssize_t count;
4646
double *xy;
47+
int mapping;
4748
} PyPathObject;
4849

4950
static PyTypeObject PyPathType;
@@ -91,6 +92,7 @@ path_new(Py_ssize_t count, double *xy, int duplicate) {
9192

9293
path->count = count;
9394
path->xy = xy;
95+
path->mapping = 0;
9496

9597
return path;
9698
}
@@ -276,6 +278,10 @@ path_compact(PyPathObject *self, PyObject *args) {
276278

277279
double cityblock = 2.0;
278280

281+
if (self->mapping) {
282+
PyErr_SetString(PyExc_ValueError, "Path compacted during mapping");
283+
return NULL;
284+
}
279285
if (!PyArg_ParseTuple(args, "|d:compact", &cityblock)) {
280286
return NULL;
281287
}
@@ -393,18 +399,21 @@ path_map(PyPathObject *self, PyObject *args) {
393399
xy = self->xy;
394400

395401
/* apply function to coordinate set */
402+
self->mapping = 1;
396403
for (i = 0; i < self->count; i++) {
397404
double x = xy[i + i];
398405
double y = xy[i + i + 1];
399406
PyObject *item = PyObject_CallFunction(function, "dd", x, y);
400407
if (!item || !PyArg_ParseTuple(item, "dd", &x, &y)) {
408+
self->mapping = 0;
401409
Py_XDECREF(item);
402410
return NULL;
403411
}
404412
xy[i + i] = x;
405413
xy[i + i + 1] = y;
406414
Py_DECREF(item);
407415
}
416+
self->mapping = 0;
408417

409418
Py_INCREF(Py_None);
410419
return Py_None;

0 commit comments

Comments
 (0)