Repository navigation
fix(fletcher32): reject an out buffer of the wrong size - #896
Conversation
Fletcher32.decode copied the payload into a caller-supplied 'out' buffer without checking its size: a smaller buffer led to an out-of-bounds write, while a larger buffer left its tail untouched (stale data). Validate the destination size before the copy and raise ValueError with the expected payload size instead. The zero-length payload case now skips the memcpy entirely, so an empty buffer is never indexed. Add tests for oversized, undersized, exact-size and bytearray destinations.
|
A note on the red CI matrix so it is not mistaken for a regression from this change. The This comes from zfpy Where the matrix is not affected by that dependency issue, the change is fully exercised by the real meson build and the full test suite:
The "Install numcodecs" step (which runs Cython + meson for |
|
im going to push some polishing changes, thanks for this |
…with JenkinsLookup3 Fold the four out tests into one parametrized happy-path test (uint8, a Fortran-ordered uint32 array, float64, bytearray, memoryview) and one parametrized test for an out of the wrong size (smaller, larger, 0-d). The exact-size ndarray and bytearray cases repeated tests in tests/test_codec_contract.py. Word the size error as JenkinsLookup3.decode does, document the out size rule in the decode docstring, and move the release note into the existing Fixes section of the Unreleased notes. Assisted-by: ClaudeCode:claude-opus-5-5
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #896 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 27
Lines 1196 1196
=========================================
Hits 1196 1196 🚀 New features to boost your workflow:
|
* Revert "feat(typing): ship py.typed and check numcodecs with strict mypy (#915)" This reverts commit dd52bc0 except in tests/test_fletcher32.py, where the tests added afterwards by #896 keep the import they need. Assisted-by: ClaudeCode:claude-fable-5.2 * Revert "fix: return the caller's out from decode(buf, out=out) in every codec (#907)" This reverts commit 1d2229f. Assisted-by: ClaudeCode:claude-fable-5.2 * docs: drop a release-note reference to reverted behavior The Fletcher32 entry compared it with the size check in `JenkinsLookup3.decode`, which the revert of #907 removes. Also sort the imports of tests/test_fletcher32.py under the restored lint configuration. Assisted-by: ClaudeCode:claude-fable-5.2
Problem
Fletcher32.decodecopies the payload into a caller-providedoutbufferwithout validating the destination size:
Two failure modes:
outsmaller than the decoded payload: out-of-bounds write past the end ofthe destination (heap corruption / crash).
outlarger than the decoded payload: the tail ofoutis left untouched,so stale/uninitialized data is silently returned to the caller.
Fix
Validate the destination size before copying:
out_mvis a 1-Duint8view, so its element count equals the number of bytes.The
if payload_len:guard also keeps a zero-length payload from indexing&out_mv[0]on an empty buffer.Tests
Added to
tests/test_fletcher32.py:test_out_too_large: oversizedoutraisesValueErrorand is not written to(checked by filling it with a sentinel first).
test_out_too_small: undersizedoutraisesValueErrorinstead of overflowing.test_out_correct_size: exact-sizeoutis filled and returned.test_out_bytearray: abytearraydestination is filled correctly.Verification
fletcher32.pyxcleanly.extension, placed it in a temporary copy of the installed package, and ran the
updated
tests/test_fletcher32.py: 10 passed.mainbuild: 2 failed, 8 passed.test_out_too_largeandtest_out_too_smallboth fail withDID NOT RAISE ValueError, confirming that the new tests catch the bug.ruff check tests/test_fletcher32.pyandruff format --check tests/test_fletcher32.pypass.ruffcannot parse.pyxfiles, so the Cython source is verified by compiling it rather than bylinting it.
MSVC/meson toolchain, and the available Python 3.10 environment (with the
matching installed package) predates the project's
requires-python >= 3.12.The standalone module build above exercises the changed code path because
fletcher32.pyxhas no NumPy C-API dependency and the_utilshelpers arecdef inline.Relation to other PRs
payloads; it still copies without a size check. This change is complementary.
Both touch the same few lines, so merging one after the other may need a trivial
conflict resolution; the
if payload_len:guard here overlaps with Fix Fletcher32 for empty buffers #889'sb_len > FOOTER_LENGTHcondition but does not conflict semantically.JenkinsLookup3/Checksum32.Fletcher32is a separate Cythonimplementation and is not covered there.
Checklist
docs/release.md