summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2021-11-12 17:35:36 -0600
committerCharles Harris <charlesr.harris@gmail.com>2021-11-25 15:13:50 -0700
commit75fd6a71faddc10dad477cc60bf594d6a714f220 (patch)
tree16631920fe9a179000f9bdc304d9a2d22196c48e
parent27a33ccc59a5d84c7683120552c41c237c919322 (diff)
downloadnumpy-75fd6a71faddc10dad477cc60bf594d6a714f220.tar.gz
BUG: Fix failure to create aligned, empty structured dtype
This fixes a SIGFPE exception due to division/modulo by zero when an empty structured dtype is requested aligned.
-rw-r--r--numpy/core/src/multiarray/descriptor.c2
-rw-r--r--numpy/core/tests/test_dtype.py6
2 files changed, 7 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c
index b8b477e5d..fa24f1a87 100644
--- a/numpy/core/src/multiarray/descriptor.c
+++ b/numpy/core/src/multiarray/descriptor.c
@@ -1325,7 +1325,7 @@ _convert_from_dict(PyObject *obj, int align)
goto fail;
}
/* If align is set, make sure the alignment divides into the size */
- if (align && itemsize % new->alignment != 0) {
+ if (align && new->alignment > 0 && itemsize % new->alignment != 0) {
PyErr_Format(PyExc_ValueError,
"NumPy dtype descriptor requires alignment of %d bytes, "
"which is not divisible into the specified itemsize %d",
diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py
index 1dcf1fb8e..3cb0c2b84 100644
--- a/numpy/core/tests/test_dtype.py
+++ b/numpy/core/tests/test_dtype.py
@@ -621,6 +621,12 @@ class TestSubarray:
t2 = np.dtype('2i4', align=True)
assert_equal(t1.alignment, t2.alignment)
+ def test_aligned_empty(self):
+ # Mainly regression test for gh-19696: construction failed completely
+ dt = np.dtype([], align=True)
+ assert dt == np.dtype([])
+ dt = np.dtype({"names": [], "formats": [], "itemsize": 0}, align=True)
+ assert dt == np.dtype([])
def iter_struct_object_dtypes():
"""