Buffer recovery
@@ -88,14 +88,23 @@ impl MemoryManager {
let body_clone = self.inner.clone_mbuf(mbuf_ptr);
// Wrap the mbuf first so we free it on early exit.
- let mbuf = Mbuf { ptr: body_clone, mm: self.clone() };
+ let mut mbuf = Mbuf { ptr: body_clone, mm: self.clone() };
- // Bounds check the length.
- todo!();
+ let orig_ptr = mbuf.data_ptr();
+ let orig_len = mbuf.len();
- // Adjust and trim the mbuf to represent the subslice of the original body allocation passed
- // in by the application.
- todo!();
+ if (ptr as usize) < (orig_ptr as usize) {
+ anyhow::bail!("Trying to recover data pointer outside original body: {:?} vs. {:?}", ptr, orig_ptr);
+ }
+ let adjust = ptr as usize - orig_ptr as usize;
+
+ if adjust + len > orig_len {
+ anyhow::bail!("Recovering too many bytes: {} + {} > {}", adjust, len, orig_len);
+ }
+ let trim = orig_len - (adjust + len);
+
+ mbuf.adjust(adjust);
+ mbuf.trim(trim);
Ok(mbuf)
}
