Skip to content

Commit ce56683

Browse files
committed
fix: check ptr nullity before calling from_raw_parts
1 parent 7cf345c commit ce56683

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/buf.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ impl Buf {
4444
impl Deref for Buf {
4545
type Target = [u8];
4646
fn deref(&self) -> &[u8] {
47+
if self.raw.ptr.is_null() {
48+
return &[];
49+
}
4750
unsafe { slice::from_raw_parts(self.raw.ptr as *const u8, self.raw.size as usize) }
4851
}
4952
}
5053

5154
impl DerefMut for Buf {
5255
fn deref_mut(&mut self) -> &mut [u8] {
56+
if self.raw.ptr.is_null() {
57+
return &mut [];
58+
}
5359
unsafe { slice::from_raw_parts_mut(self.raw.ptr as *mut u8, self.raw.size as usize) }
5460
}
5561
}

0 commit comments

Comments
 (0)