Skip to content

Commit 142deef

Browse files
committed
Fixed invariant violation in MemBio::get_buf with empty results
Pointer arguments to `slice::from_raw_parts` are required to be non-null. (See https://davidben.net/2024/01/15/empty-slices.html for details.)
1 parent 32f150b commit 142deef

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

openssl/src/bio.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ impl MemBio {
6363
unsafe {
6464
let mut ptr = ptr::null_mut();
6565
let len = ffi::BIO_get_mem_data(self.0, &mut ptr);
66-
slice::from_raw_parts(ptr as *const _ as *const _, len as usize)
66+
if len == 0 {
67+
&[]
68+
} else {
69+
slice::from_raw_parts(ptr as *const _ as *const _, len as usize)
70+
}
6771
}
6872
}
6973

@@ -83,3 +87,14 @@ cfg_if! {
8387
}
8488
}
8589
}
90+
91+
#[cfg(test)]
92+
mod tests {
93+
use super::MemBio;
94+
95+
#[test]
96+
fn test_mem_bio_get_buf_empty() {
97+
let b = MemBio::new().unwrap();
98+
assert_eq!(b.get_buf(), &[]);
99+
}
100+
}

0 commit comments

Comments
 (0)