Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 45 additions & 17 deletions pkgs/id/src/event_store/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,25 +407,26 @@ fn put_varint(out: &mut Vec<u8>, mut x: usize) {
out.push(x as u8);
}

/// Refuses a bound longer than a key. The decoder rejects such bounds
/// (see Reader::entry), so an encoder that wrote one would produce a
/// frame its own reader refuses — and a bound of 256+ bytes would wrap
/// the u8 length prefix encode_entry writes and garble the entry
/// stream. Every encode-side gate shares this check (#2595).
/// Refuses a finite bound outside `1..=KEY_LEN`. Length zero is reserved
/// for the +infinity sentinel, and longer bounds are rejected by the
/// decoder (see Reader::entry); bounds of 256+ bytes would also wrap the
/// u8 length prefix and garble the entry stream. Every encode-side gate
/// shares this check (#2595, #2607).
fn check_bound_len(bound: &[u8]) -> Result<()> {
ensure!(
bound.len() <= KEY_LEN,
"a bound of {} bytes is longer than a key ({KEY_LEN} bytes)",
(1..=KEY_LEN).contains(&bound.len()),
"a finite bound must contain 1..={KEY_LEN} bytes; use None for +infinity (got {} bytes)",
bound.len()
);
Ok(())
}

fn encode_entry(e: &Entry) -> Vec<u8> {
fn encode_entry(e: &Entry) -> Result<Vec<u8>> {
let mut out = Vec::new();
match &e.upper {
None => out.push(0),
Some(b) => {
check_bound_len(b)?;
out.push(b.len() as u8);
out.extend_from_slice(b);
}
Expand All @@ -445,25 +446,22 @@ fn encode_entry(e: &Entry) -> Vec<u8> {
}
}
}
out
Ok(out)
}

/// Splits a message into frames of at most `budget` bytes each. Every frame but the last is
/// marked `more`. The entries keep their order, so the receiver reads them back as one list.
///
/// # Errors
///
/// Refuses an entry that does not fit a frame by itself, and an entry
/// whose upper bound is longer than a key.
/// Refuses an entry that does not fit a frame by itself, and a finite
/// upper bound outside `1..=KEY_LEN` bytes.
pub fn frames(msg: &[Entry], budget: usize) -> Result<Vec<Vec<u8>>> {
let mut out = Vec::new();
let mut body: Vec<u8> = Vec::new();
let mut count = 0usize;
for entry in msg {
if let Some(bound) = &entry.upper {
check_bound_len(bound)?;
}
let encoded = encode_entry(entry);
let encoded = encode_entry(entry)?;
ensure!(
encoded.len() + FRAME_HEADER_RESERVE <= budget,
"an entry of {} bytes does not fit a {budget}-byte frame",
Expand Down Expand Up @@ -598,8 +596,8 @@ pub fn frame_entry_count(bytes: &[u8]) -> Result<usize> {
///
/// # Errors
///
/// Refuses a bound that does not increase, a bound longer than a key,
/// and an entry after +infinity.
/// Refuses a bound outside `1..=KEY_LEN` bytes, a bound that does not
/// increase, and an entry after +infinity.
pub fn check_bounds(msg: &[Entry]) -> Result<()> {
let mut prev: Option<&[u8]> = None;
let mut ended = false;
Expand Down Expand Up @@ -1101,6 +1099,36 @@ mod tests {
}
}

#[test]
fn test_empty_bounds_are_refused_but_one_byte_and_infinity_round_trip() {
let skip = |upper: Option<Vec<u8>>| Entry {
upper,
mode: Mode::Skip,
};
let empty = skip(Some(Vec::new()));
let encode_error = frames(&[empty.clone()], 4096).unwrap_err();
assert!(
encode_error.to_string().contains("1..=72 bytes"),
"frames error names the finite-bound range: {encode_error}"
);
let bounds_error = check_bounds(&[empty]).unwrap_err();
assert!(
bounds_error.to_string().contains("1..=72 bytes"),
"check_bounds error names the finite-bound range: {bounds_error}"
);

let one_byte = skip(Some(vec![1]));
let wire = frames(&[one_byte.clone()], 4096).unwrap();
let (_, decoded) = decode_frame(&wire[0]).unwrap();
assert_eq!(decoded, [one_byte]);

let infinity = skip(None);
let wire = frames(&[infinity.clone()], 4096).unwrap();
assert_eq!(wire[0][2], 0, "zero length remains the +infinity sentinel");
let (_, decoded) = decode_frame(&wire[0]).unwrap();
assert_eq!(decoded, [infinity]);
}

#[test]
fn test_frames_fit_the_budget_and_read_back_in_order() {
let p = party(set(9, 3000, false));
Expand Down
Loading