Repository navigation
Fix Fletcher32 for empty buffers - #889
RaphaelFakhri wants to merge 1 commit into
Conversation
49e295d to
db932db
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #889 +/- ##
=======================================
Coverage 89.71% 89.71%
=======================================
Files 27 27
Lines 914 914
=======================================
Hits 820 820
Misses 94 94 🚀 New features to boost your workflow:
|
Duo-ee
left a comment
There was a problem hiding this comment.
The empty-buffer fix looks right, but decode still has no size check on out, and that one is a memory-safety problem rather than a silent repeat:
outsmaller thanb_len - FOOTER_LENGTH→memcpywrites past the destination (heap corruption / crash).outlarger → the payload is copied, the tail of the caller's buffer is left untouched, anddecodestill reports success.ensure_contiguous_ndarray(out)isnp.asarray(out)for non-array buffers, so abytearrayoutbecomes read-only and&out_mv[0]points at read-only memory.
A ValueError before the copy covers all three cases:
payload_len = b_len - FOOTER_LENGTH
out_mv = ensure_contiguous_ndarray(out).view("uint8")
if out_mv.size != payload_len:
raise ValueError(
f"Output buffer has {out_mv.size} bytes, but the decoded payload has {payload_len} bytes."
)
memcpy(&out_mv[0], b_ptr, payload_len)Worth adding a case with an oversized out and a short one next to test_empty.
|
@Duo-ee The undersized Two notes on the details. A |
Fletcher32.encoderaisesIndexErrorfor an empty buffer, andFletcher32.decoderaisesIndexErrorfor a buffer that holds only the 4-byte checksum. The codec takes&mv[0]of an empty memoryview, which fails Cython's bounds check.This change:
_fletcher32for empty input, which matches the HDF5/netCDF reference implementation.memcpyinencodeand in theoutbranch ofdecodewhen the payload is empty.test_empty, which round-trips an empty buffer with and withoutout.Closes #888