From 1301571ca2775d08831e4b465c5848403ab40199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20B=C3=A1rias?= Date: Fri, 8 Mar 2024 22:52:48 +0000 Subject: [PATCH 1/2] BUG: raise error for negative-sized dtype Closes #25860 --- numpy/_core/src/multiarray/descriptor.c | 5 +++++ numpy/_core/tests/test_dtype.py | 3 +++ numpy/_core/tests/test_numeric.py | 3 +-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/numpy/_core/src/multiarray/descriptor.c b/numpy/_core/src/multiarray/descriptor.c index c77b380e9386..8a8e432d82ee 100644 --- a/numpy/_core/src/multiarray/descriptor.c +++ b/numpy/_core/src/multiarray/descriptor.c @@ -1813,6 +1813,11 @@ _convert_from_str(PyObject *obj, int align) /* Parse the integer, make sure it's the rest of the string */ elsize = (int)strtol(type + 1, &typeend, 10); + /* Make sure size is not negative */ + if (elsize < 0) { + goto fail; + } + if (typeend - type == len) { kind = type[0]; diff --git a/numpy/_core/tests/test_dtype.py b/numpy/_core/tests/test_dtype.py index 664f4e028151..0595fb60240f 100644 --- a/numpy/_core/tests/test_dtype.py +++ b/numpy/_core/tests/test_dtype.py @@ -96,6 +96,9 @@ def test_invalid_types(self): assert_raises(TypeError, np.dtype, 'q8') assert_raises(TypeError, np.dtype, 'Q8') + # Make sure negative-sized dtype raises an error + assert_raises(TypeError, np.dtype, 'S-1') + def test_richcompare_invalid_dtype_equality(self): # Make sure objects that cannot be converted to valid # dtypes results in False/True when compared to valid dtypes. diff --git a/numpy/_core/tests/test_numeric.py b/numpy/_core/tests/test_numeric.py index 3acbb20a1619..4dfc45cb180a 100644 --- a/numpy/_core/tests/test_numeric.py +++ b/numpy/_core/tests/test_numeric.py @@ -8,7 +8,6 @@ import numpy as np from numpy._core import umath, sctypes -from numpy._core._exceptions import _ArrayMemoryError from numpy._core.numerictypes import obj2sctype from numpy._core.arrayprint import set_string_function from numpy.exceptions import AxisError @@ -3345,7 +3344,7 @@ class MyNDArray(np.ndarray): assert_(type(b) is not MyNDArray) # Test invalid dtype - with assert_raises(_ArrayMemoryError): + with assert_raises(TypeError): a = np.array(b"abc") like_function(a, dtype="S-1", **fill_kwarg) From 695733ad16491a78506f7a9e94faea057eb56a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20B=C3=A1rias?= Date: Mon, 11 Mar 2024 19:51:22 +0000 Subject: [PATCH 2/2] TST: add two new tests for negative-sized dtypes --- numpy/_core/tests/test_dtype.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/numpy/_core/tests/test_dtype.py b/numpy/_core/tests/test_dtype.py index 0595fb60240f..eb4f915ee452 100644 --- a/numpy/_core/tests/test_dtype.py +++ b/numpy/_core/tests/test_dtype.py @@ -98,6 +98,8 @@ def test_invalid_types(self): # Make sure negative-sized dtype raises an error assert_raises(TypeError, np.dtype, 'S-1') + assert_raises(TypeError, np.dtype, 'U-1') + assert_raises(TypeError, np.dtype, 'V-1') def test_richcompare_invalid_dtype_equality(self): # Make sure objects that cannot be converted to valid