Guarantee address in slice() for empty slices. (#780)

This commit is contained in:
Michael Färber
2025-05-20 14:52:05 +02:00
committed by GitHub
parent 5c90b11415
commit d9d5c59ef8
2 changed files with 21 additions and 1 deletions
+1 -1
View File
@@ -401,7 +401,7 @@ impl Bytes {
);
if end == begin {
return Bytes::new();
return Bytes::new_empty_with_ptr(self.ptr.wrapping_add(begin));
}
let mut ret = self.clone();
+20
View File
@@ -1421,6 +1421,26 @@ fn try_reclaim_arc() {
assert_eq!(true, buf.try_reclaim(6));
}
#[test]
fn slice_empty_addr() {
let buf = Bytes::from(vec![0; 1024]);
let ptr_start = buf.as_ptr();
let ptr_end = ptr_start.wrapping_add(1024);
let empty_end = buf.slice(1024..);
assert_eq!(empty_end.len(), 0);
assert_eq!(empty_end.as_ptr(), ptr_end);
let empty_start = buf.slice(..0);
assert_eq!(empty_start.len(), 0);
assert_eq!(empty_start.as_ptr(), ptr_start);
// Is miri happy about the provenance?
let _ = &empty_end[..];
let _ = &empty_start[..];
}
#[test]
fn split_off_empty_addr() {
let mut buf = Bytes::from(vec![0; 1024]);