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

Do not segfault in svd(a) with VT.size > INT_MAX #20349

Merged
merged 2 commits into from Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions scipy/linalg/_decomp_svd.py
Expand Up @@ -116,6 +116,20 @@ def svd(a, full_matrices=True, compute_uv=True, overwrite_a=False,
if lapack_driver not in ('gesdd', 'gesvd'):
message = f'lapack_driver must be "gesdd" or "gesvd", not "{lapack_driver}"'
raise ValueError(message)

if lapack_driver == 'gesdd' and compute_uv:
# XXX: revisit int32 when ILP64 lapack becomes a thing
ev-br marked this conversation as resolved.
Show resolved Hide resolved
max_mn, min_mn = (m, n) if m > n else (n, m)
if full_matrices:
if max_mn*max_mn > numpy.iinfo(numpy.int32).max:
raise ValueError(f"Indexing a matrix size {max_mn} x {max_mn} "
f" would incur integer overflow in LAPACK.")
ev-br marked this conversation as resolved.
Show resolved Hide resolved
else:
sz = max(m * min_mn, n * min_mn)
if max(m * min_mn, n * min_mn) > numpy.iinfo(numpy.int32).max:
raise ValueError(f"Indexing a matrix of {sz} elements would "
"incur an in integer overflow in LAPACK.")

funcs = (lapack_driver, lapack_driver + '_lwork')
gesXd, gesXd_lwork = get_lapack_funcs(funcs, (a1,), ilp64='preferred')

Expand Down
8 changes: 8 additions & 0 deletions scipy/linalg/tests/test_decomp.py
Expand Up @@ -1089,6 +1089,14 @@ class TestSVD_GESVD(TestSVD_GESDD):
lapack_driver = 'gesvd'


def test_svd_gesdd_nofegfault():
# svd(a) with {U,VT}.size > INT_MAX does not segfault
# cf https://github.com/scipy/scipy/issues/14001
df=np.ones((4799, 53130), dtype=np.float64)
with assert_raises(ValueError):
svd(df)


class TestSVDVals:

def test_empty(self):
Expand Down