Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: scipy.spatial.Delaunay, scipy.interpolate.LinearNDInterpolator segfault on invalid shapes #20623

Closed
lpsinger opened this issue May 1, 2024 · 4 comments
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.interpolate scipy.spatial
Milestone

Comments

@lpsinger
Copy link
Contributor

lpsinger commented May 1, 2024

Describe your issue.

scipy.spatial.Delaunay and scipy.interpolate.LinearNDInterpolator segfault on inputs of invalid shapes.

Reproducing Code Example

>>> import numpy as np
>>> from scipy.spatial import Delaunay
>>> Delaunay(np.random.randn(4, 10, 3))

or

>>> import numpy as np
>>> from scipy.interpolate import LinearNDInterpolator
>>> LinearNDInterpolator(np.random.randn(4, 10, 3), np.random.randn(10))

Error message

Segmentation fault: 11

SciPy/NumPy/Python version and system information

1.13.0 1.26.4 sys.version_info(major=3, minor=11, micro=9, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= SANDYBRIDGE MAX_THREADS=64
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.26.dev
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= SANDYBRIDGE MAX_THREADS=64
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.26.dev
  pybind11:
    detection method: config-tool
    include directory: unknown
    name: pybind11
    version: 2.12.0
Compilers:
  c:
    commands: cc
    linker: ld64
    name: clang
    version: 13.0.0
  c++:
    commands: c++
    linker: ld64
    name: clang
    version: 13.0.0
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.10
  fortran:
    commands: gfortran
    linker: ld64
    name: gcc
    version: 11.3.0
  pythran:
    include directory: ../../../../../../private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/pip-build-env-mo36y8b_/overlay/lib/python3.11/site-packages/pythran
    version: 0.15.0
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: darwin
  cross-compiled: false
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: darwin
Python Information:
  path: /private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/cibw-run-1iia41ej/cp311-macosx_x86_64/build/venv/bin/python
  version: '3.11'
@lpsinger lpsinger added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label May 1, 2024
@lucascolley
Copy link
Member

I see Qhull and segfault - @andfoy related to gh-20619, maybe?

@andfoy
Copy link
Contributor

andfoy commented May 2, 2024

This issue is not related to #20619, since it is not related to concurrency. However, I can reproduce it, which implies that some other kind of error is occurring, I can further investigate this one.

tylerjereddy added a commit to tylerjereddy/scipy that referenced this issue May 2, 2024
* Deals with the `spatial` part of scipygh-20623 (`Voronoi`
was also affected beyond the originally-reported `Delaunay`
segfault).

* Both classes are documented to accept arrays with two dimensions
only, so raise a `ValueError` in cases with other dimensions to
avoid the segfault.

* Other potential points of confusion here are the differences
between arrays with two dimensions and two dimensional arrays
that represent generators with more than two dimensions via
the number of columns, but this is just how things are for
most array programming languages of course. Also, the docs
don't explicitly say array-like for the generators I don't think, but this patch
only worked if I placed it after the coercion to array type.

[skip circle]
@tylerjereddy
Copy link
Contributor

As the cross-linked PR indicates, we just need to error out for the spatial cases since those are invalid inputs. I didn't touch the interpolate case, though it may or may not benefit from my patch if it passes through the same code path (didn't check).

@AtsushiSakai
Copy link
Member

In the interpolate case, the LinearNDInterpolator is using the qhull.Delaunay in its initializer.

def _calculate_triangulation(self, points):
self.tri = qhull.Delaunay(points)

In the latest code, I have confirmed that using the code reported in this issue does not cause a segmentation fault, thanks to the validation introduced in PR #20633:

In [1]: import numpy as np

In [2]: from scipy.interpolate import LinearNDInterpolator

In [3]: LinearNDInterpolator(np.random.randn(4, 10, 3), np.random.randn(10))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 LinearNDInterpolator(np.random.randn(4, 10, 3), np.random.randn(10))

File interpnd.pyx:326, in scipy.interpolate.interpnd.LinearNDInterpolator.__init__()

File interpnd.pyx:92, in scipy.interpolate.interpnd.NDInterpolatorBase.__init__()

File interpnd.pyx:330, in scipy.interpolate.interpnd.LinearNDInterpolator._calculate_triangulation()

File _qhull.pyx:1808, in scipy.spatial._qhull.Delaunay.__init__()

ValueError: Input points array must have 2 dimensions.

So, I think we can close this issue.

@AtsushiSakai AtsushiSakai added this to the 1.14.0 milestone May 5, 2024
@tylerjereddy tylerjereddy modified the milestones: 1.14.0, 1.13.1 May 5, 2024
tylerjereddy added a commit to tylerjereddy/scipy that referenced this issue May 5, 2024
* Deals with the `spatial` part of scipygh-20623 (`Voronoi`
was also affected beyond the originally-reported `Delaunay`
segfault).

* Both classes are documented to accept arrays with two dimensions
only, so raise a `ValueError` in cases with other dimensions to
avoid the segfault.

* Other potential points of confusion here are the differences
between arrays with two dimensions and two dimensional arrays
that represent generators with more than two dimensions via
the number of columns, but this is just how things are for
most array programming languages of course. Also, the docs
don't explicitly say array-like for the generators I don't think, but this patch
only worked if I placed it after the coercion to array type.

[skip circle] [skip ci] [ci skip]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.interpolate scipy.spatial
Projects
None yet
Development

No branches or pull requests

5 participants