Repository navigation
Commit 74a3f56
authored
Reject aux notes that can't be written in wrann (#605)
`wrann` writes each aux note as a one-byte length followed by one byte
per character, but it doesn't check that the note fits. The bytes are
cast to `uint8`, so values above 255 wrap silently:
```python
wfdb.wrann("a", "atr", sample=np.array([1, 2]), symbol=["+", "N"], aux_note=["x" * 256, ""])
len(wfdb.rdann("a", "atr").sample) # 130 annotations instead of 2
wfdb.wrann("a", "atr", sample=np.array([1]), symbol=["+"], aux_note=["(€)"])
wfdb.rdann("a", "atr").aux_note # ['(¬)']
```
`rdann` decodes each byte with `chr`, i.e. as Latin-1, so the notes that
round-trip are those of at most 255 Latin-1 characters (the length byte
also limits notes to 255 bytes in the WFDB format).
Changes:
- `check_field` now raises a `ValueError` for an aux note that is longer
than 255 characters or has characters outside Latin-1, before anything
is written.
- `field2bytes` writes `value.encode("latin-1")` and takes the length
from the encoded bytes. For valid notes the output is the same as
before.
- The `wrann` docstring mentions the limits, and there's a changelog
entry.
Tests: a round trip with a 255-character note and Latin-1 characters
(`°`, `ü`), and both kinds of invalid note raising the error without
creating a file. The invalid-note tests fail on `main`. `pytest -m "not
network"` passes (99 tests), and the docs build passes.3 files changed
Lines changed: 59 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
44 | 47 | | |
45 | 48 | | |
46 | 49 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
544 | 544 | | |
545 | 545 | | |
546 | 546 | | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
547 | 563 | | |
548 | 564 | | |
549 | 565 | | |
| |||
1670 | 1686 | | |
1671 | 1687 | | |
1672 | 1688 | | |
1673 | | - | |
| 1689 | + | |
| 1690 | + | |
1674 | 1691 | | |
1675 | | - | |
| 1692 | + | |
1676 | 1693 | | |
1677 | 1694 | | |
1678 | 1695 | | |
| |||
1727 | 1744 | | |
1728 | 1745 | | |
1729 | 1746 | | |
1730 | | - | |
| 1747 | + | |
| 1748 | + | |
1731 | 1749 | | |
1732 | 1750 | | |
1733 | 1751 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
287 | 288 | | |
288 | 289 | | |
289 | 290 | | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
0 commit comments