Skip to content

Commit e1a0358

Browse files
authored
fix(security): use of assert statements for input validation in p (#435)
The file `dask_image/ndfourier/_utils.py` uses Python `assert` statements for input validation (e.g., lines checking `issubclass(dtype, numbers.Real)`). Assert statements can be disabled globally when Python is run with the `-O` (optimize) flag, which would bypass these checks entirely. This could allow invalid inputs to propagate through the code, potentially causing crashes, incorrect results, or undefined behavior in production environments where optimized Python execution is used. Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
1 parent 6a7c19c commit e1a0358

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

dask_image/ndfourier/_utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88

99
def _get_freq_grid(shape, chunks, axis, n, dtype=float):
10-
assert len(shape) == len(chunks)
10+
if len(shape) != len(chunks):
11+
raise ValueError("shape and chunks must have the same length")
1112

1213
shape = tuple(shape)
1314
dtype = np.dtype(dtype).type
1415

15-
assert (issubclass(dtype, numbers.Real) and
16-
not issubclass(dtype, numbers.Integral))
16+
if not (issubclass(dtype, numbers.Real) and
17+
not issubclass(dtype, numbers.Integral)):
18+
raise TypeError("dtype must be a non-integral real number")
1719

1820
axis = axis % len(shape)
1921

@@ -33,8 +35,9 @@ def _get_freq_grid(shape, chunks, axis, n, dtype=float):
3335
def _get_ang_freq_grid(shape, chunks, axis, n, dtype=float):
3436
dtype = np.dtype(dtype).type
3537

36-
assert (issubclass(dtype, numbers.Real) and
37-
not issubclass(dtype, numbers.Integral))
38+
if not (issubclass(dtype, numbers.Real) and
39+
not issubclass(dtype, numbers.Integral)):
40+
raise TypeError("dtype must be a non-integral real number")
3841

3942
pi = dtype(np.pi)
4043

0 commit comments

Comments
 (0)