Skip to content

Commit 7b27f3a

Browse files
committed
Check that array shape is not larger than MAX_DIM
1 parent 3a399b1 commit 7b27f3a

File tree

4 files changed

+10
-0
lines changed

4 files changed

+10
-0
lines changed

src/blosc2/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class Tuner(Enum):
109109
GLOBAL_REGISTERED_CODECS_STOP,
110110
MAX_BLOCKSIZE,
111111
MAX_BUFFERSIZE,
112+
MAX_DIM,
112113
MAX_OVERHEAD,
113114
MAX_TYPESIZE,
114115
MIN_HEADER_LENGTH,

src/blosc2/blosc2_ext.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ MAX_TYPESIZE = BLOSC2_MAXTYPESIZE
542542
MAX_BUFFERSIZE = BLOSC2_MAX_BUFFERSIZE
543543
MAX_BLOCKSIZE = BLOSC2_MAXBLOCKSIZE
544544
MAX_OVERHEAD = BLOSC2_MAX_OVERHEAD
545+
MAX_DIM = B2ND_MAX_DIM
545546
VERSION_STRING = (<char*>BLOSC2_VERSION_STRING).decode("utf-8")
546547
VERSION_DATE = (<char*>BLOSC2_VERSION_DATE).decode("utf-8")
547548
MIN_HEADER_LENGTH = BLOSC_MIN_HEADER_LENGTH

src/blosc2/ndarray.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,8 @@ def _check_shape(shape):
29142914
shape = (shape,)
29152915
elif not isinstance(shape, tuple | list):
29162916
raise TypeError("shape should be a tuple or a list!")
2917+
if len(shape) > blosc2.MAX_DIM:
2918+
raise ValueError(f"shape length {len(shape)} is too large (>{blosc2.MAX_DIM})!")
29172919
return shape
29182920

29192921

tests/ndarray/test_zeros.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,9 @@ def test_shape_empty():
166166
assert a.dtype == np.int32
167167
b = np.zeros((), dtype=np.int32)
168168
np.testing.assert_equal(a[()], b)
169+
170+
171+
def test_shape_max_dims():
172+
# Test that the shape cannot exceed the maximum number of dimensions
173+
with pytest.raises(ValueError):
174+
a = blosc2.zeros((1,) * (blosc2.MAX_DIM + 1), dtype=np.int32)

0 commit comments

Comments
 (0)