Skip to content

Commit 8806e99

Browse files
moulinsHerschel
authored andcommitted
wstr: Fix provenance in WString::from_buf_unchecked
Get a pointer with provenance over the whole buffer, instead of a pointer with provenance only to the initialized part.
1 parent a678a39 commit 8806e99

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

wstr/src/buf.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@ impl WString {
5050
pub unsafe fn from_buf_unchecked(buf: Units<Vec<u8>, Vec<u16>>) -> Self {
5151
// SAFETY: we take ownership of the buffer; avoid double frees
5252
let mut buf = ManuallyDrop::new(buf);
53-
let (cap, ptr) = match buf.deref_mut() {
54-
Units::Bytes(buf) => (buf.capacity(), Units::Bytes(&mut buf[..] as *mut _)),
55-
Units::Wide(buf) => (buf.capacity(), Units::Wide(&mut buf[..] as *mut _)),
53+
let (cap, len, ptr, is_wide) = match buf.deref_mut() {
54+
Units::Bytes(buf) => (buf.capacity(), buf.len(), buf.as_mut_ptr() as *mut _, false),
55+
Units::Wide(buf) => (buf.capacity(), buf.len(), buf.as_mut_ptr() as *mut _, true),
5656
};
5757

58-
let wstr = ptr::from_units(ptr);
5958
Self {
60-
data: NonNull::new_unchecked(ptr::data(wstr)),
61-
meta: ptr::metadata(wstr),
59+
data: NonNull::new_unchecked(ptr),
60+
meta: ptr::WStrMetadata::new(len, is_wide),
6261
capacity: cap as u32,
6362
}
6463
}

0 commit comments

Comments
 (0)