Repository navigation
Commit c7d7acc
authored
Avoid integer overflow in normalize_bound (#681)
`normalize_bound()` subtracts NumPy integer extrema in their original
dtype. A signed signal whose range exceeds that dtype can wrap before
the scaling coefficient is computed.
```python
sig = np.array([-30000, 0, 30000], dtype=np.int16)
processing.normalize_bound(sig, -1, 1)
# before: [22.67630058, 11.83815029, 1.0]
# after: [-1.0, 0.0, 1.0]
```
Widen integer extrema to Python integers before computing their
difference. Use `np.multiply` for scaling so integer list inputs remain
supported after the coefficient becomes a Python float. The
floating-input formula is unchanged.
The 19 added regression cases cover signed int8/int16/int32/int64 ranges
and three bound pairs, unsigned endpoints, float behavior, list input,
unchanged input arrays, and an int16 digital signal written and read
through the WFDB API. On unchanged code, 14 fail and 5 controls pass.
Validation on current main 05db3a2 plus this patch:
- `tests/test_processing.py tests/test_normalize_bound.py`: 153 passed
on Python 3.12.16.
- Ruff 0.17.3 check and format passed for changed Python files.
- Strict Sphinx HTML build with `-W --keep-going` passed.
Earlier validation of the same change: 28 normalization-related tests
passed on Python 3.10.12 and 1,000 seeded float-input comparisons were
byte-identical to the original formula. The full repository suite and
remote CI have not been verified. This fixes arithmetic overflow; it
does not promise exact integer resolution beyond floating-point
precision or change constant-signal/nonfinite behavior. No clinical
validation is claimed.
AI assistance was used to develop and test this change. Independent
review has not been completed.3 files changed
Lines changed: 69 additions & 1 deletion
File tree
- changelog.d
- src/wfdb/processing
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
189 | 192 | | |
190 | 193 | | |
191 | | - | |
| 194 | + | |
192 | 195 | | |
193 | 196 | | |
194 | 197 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
0 commit comments