Use checked arithmetic for frame header totals - #133
Open
cf-rhett wants to merge 1 commit into
Open
Conversation
`FramedProducer::grant` sizes its request as `sz + size_of::<H>()` and `FramedConsumer::read` validates a frame as `size_of::<H>() + hdr <= grant_len`. Both sums are unchecked. `H = u16` cannot overflow either one, but `H = usize` can, and the two failure modes are worse than a rejected grant. On the write side the wrapped total asks the coordinator for a handful of bytes while the returned grant still carries the caller's original `sz` in its header, which is the length `Deref`/`DerefMut` hand to `slice::from_raw_parts`. On the read side the wrap lands under `grant_len` and so satisfies the very check that exists to reject an inconsistent header. Debug builds catch both as an overflow panic; release builds do not.
This was referenced Jul 31, 2026
cf-rhett
marked this pull request as ready for review
July 31, 2026 19:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
FramedProducer::grantsizes its request assz + size_of::<H>(), andFramedConsumer::readvalidates a frame withsize_of::<H>() + hdr <= grant_len. Both sums are unchecked. WithH = u16neither can overflow; withH = usizeboth can.On the write side, a wrapped total asks the coordinator for a handful of bytes while the returned
FramedGrantWstill carries the caller's originalszinhdr— which is the lengthDeref/DerefMutpass toslice::from_raw_parts. The grant hands out a slice far larger than the allocation it covers.On the read side the wrap lands under
grant_len, satisfying the very check that exists to reject an inconsistent header.FramedGrantR::Derefthen builds a slice from the unwrapped header value.Debug builds catch both as an overflow panic. Release builds do not.
Both call sites already return
Result, so this needs no API change. Two regression tests are included; both fail without the fix.