Skip to content

Conversation

@TerryTaoYY
Copy link
Contributor

Summary

Fix Octets::slice_last() and OctetsMut::slice_last() returning an incorrect tail slice when the internal offset has been advanced.

These helpers previously used cap() (remaining bytes after off) to compute the start index, but then applied that index to the full underlying buffer (self.buf[...]). When off > 0, this could include already-consumed bytes and return the wrong region.

Reproduction

let mut b = Octets::with_slice(b"helloworld");
b.get_bytes(5).unwrap(); // advance offset past "hello"
assert_eq!(b.slice_last(4).unwrap(), b"orld");

Before this change, slice_last(4) could return the wrong region (e.g. starting at index 1), because cap() was treated as an absolute index into the full buffer.

Fix

Compute end as self.buf.len() and slice self.buf[end - len..end] (and the mutable equivalent). The existing len <= self.cap() guard ensures end - len >= off, so the returned slice stays within the remaining window.

Tests

Add regression cases that advance the offset via get_bytes() and then assert slice_last() returns the correct tail bytes for both Octets and OctetsMut.

Tested with:

  • \ cargo test -p octets

@TerryTaoYY TerryTaoYY requested a review from a team as a code owner December 18, 2025 04:35
@ghedo ghedo force-pushed the fix/octets-slice-last-offset branch from 758bf28 to 987f96c Compare January 15, 2026 15:19
@ghedo ghedo merged commit ad1e4da into cloudflare:master Jan 15, 2026
26 checks passed
@ghedo
Copy link
Member

ghedo commented Jan 15, 2026

Merged, thanks @TerryTaoYY!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants