Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/pyift/livewire.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/pyift_c_ext/_livewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "set.h"

#include <float.h>
#include <limits.h>

#ifndef M_SQRT2
#define M_SQRT2 1.41421356237309504880
Expand Down Expand Up @@ -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};
Expand Down
41 changes: 41 additions & 0 deletions src/pyift_c_ext/_shortestpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "heap.h"

#include <float.h>
#include <limits.h>


static inline double square(double x) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down