diff --git a/src/pyift/livewire.py b/src/pyift/livewire.py index 1f2f0ed..2ca7a23 100644 --- a/src/pyift/livewire.py +++ b/src/pyift/livewire.py @@ -187,6 +187,9 @@ def select(self, position: Union[Sequence[int], int]) -> None: if not isinstance(position, int): raise TypeError('`position` must be a integer, tuple or list.') + if not (0 <= position < self.size[0] * self.size[1] or position == self.source == -1): + raise ValueError('Index out of image boundary, {}'.format(self.size)) + if self.source != -1: self.cancel() self.current = self._opt_path(self.source, position) diff --git a/src/pyift_c_ext/_livewire.c b/src/pyift_c_ext/_livewire.c index 903b9e1..a1d0885 100644 --- a/src/pyift_c_ext/_livewire.c +++ b/src/pyift_c_ext/_livewire.c @@ -5,6 +5,7 @@ #include "set.h" #include +#include #ifndef M_SQRT2 #define M_SQRT2 1.41421356237309504880 @@ -46,6 +47,11 @@ PyObject *_livewirePath(PyArrayObject *image, PyArrayObject *saliency, PyArrayOb sal_d = PyArray_DIM(saliency, 2); } + if (PyArray_SIZE(image) > INT_MAX || (saliency && PyArray_SIZE(saliency) > INT_MAX)) { + PyErr_SetString(PyExc_ValueError, "`image` is too large."); + return NULL; + } + Coord shape = {.x = PyArray_DIM(image, 1), .y = PyArray_DIM(image, 0), .z = 1}; diff --git a/src/pyift_c_ext/_shortestpath.c b/src/pyift_c_ext/_shortestpath.c index d701249..e800f39 100644 --- a/src/pyift_c_ext/_shortestpath.c +++ b/src/pyift_c_ext/_shortestpath.c @@ -4,6 +4,7 @@ #include "heap.h" #include +#include static inline double square(double x) { @@ -23,6 +24,11 @@ PyObject *_seedCompetitionGrid(PyArrayObject *_image, PyArrayObject *_seeds) seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; + if (PyArray_SIZE(image) > INT_MAX || PyArray_SIZE(seeds) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` or `seeds` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; int d = -1; @@ -154,6 +160,11 @@ PyObject *_seedCompetitionGraph(PyArrayObject *_weights, PyArrayObject *_indices return NULL; } + if (PyArray_DIM(_seeds, 0) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`seeds` is too large."); + return NULL; + } + PyArrayObject *weights = NULL, *indices = NULL, *indptr = NULL, *seeds = NULL; weights = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _weights, NPY_DOUBLE, NPY_ARRAY_CARRAY_RO); indices = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _indices, NPY_LONG, NPY_ARRAY_CARRAY_RO); @@ -266,6 +277,11 @@ PyObject *_dynamicArcWeightGridRoot(PyArrayObject *_image, PyArrayObject *_seeds seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; + if (PyArray_SIZE(image) > INT_MAX || PyArray_SIZE(seeds) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` or `seeds` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; int d = -1; @@ -433,6 +449,11 @@ PyObject *_dynamicArcWeightGridLabel(PyArrayObject *_image, PyArrayObject *_seed seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; + if (PyArray_SIZE(image) > INT_MAX || PyArray_SIZE(seeds) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` or `seeds` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; int d = -1; @@ -605,6 +626,11 @@ PyObject *_dynamicArcWeightGridExpDecay(PyArrayObject *_image, PyArrayObject *_s seeds = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _seeds, NPY_LONG, NPY_ARRAY_CARRAY_RO); if (!image || !seeds) goto err1; + if (PyArray_SIZE(image) > INT_MAX || PyArray_SIZE(seeds) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` or `seeds` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; int d = -1; @@ -753,6 +779,11 @@ PyObject *_euclideanDistanceTransformGrid(PyArrayObject *_mask, PyArrayObject *_ scales = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _scales, NPY_DOUBLE, NPY_ARRAY_CARRAY_RO); if (!mask || !scales) goto err1; + if (PyArray_SIZE(mask) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`mask` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; if (PyArray_NDIM(mask) == 2) { @@ -896,6 +927,11 @@ PyObject *_watershedFromMinimaGrid(PyArrayObject *_image, PyArrayObject *_mask, scales = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _scales, NPY_DOUBLE, NPY_ARRAY_CARRAY_RO); if (!image || !mask || !H_minima || !scales) goto err1; + if (PyArray_SIZE(image) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; if (PyArray_NDIM(image) == 2) { @@ -1041,6 +1077,11 @@ PyObject *_orientedSeedCompetitionGrid(PyArrayObject *_image, PyArrayObject *_se mask = (PyArrayObject*) PyArray_FROM_OTF((PyObject*) _mask, NPY_BOOL, NPY_ARRAY_CARRAY_RO); if (!image || !seeds || !mask) goto err1; + if (PyArray_SIZE(image) > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "`image` is too large."); + goto err1; + } + Coord shape; Adjacency *adj = NULL; if (PyArray_NDIM(image) == 2) {