Skip to content

Commit

Permalink
Fixed invariant violation in MemBio::get_buf with empty results
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
alex committed Jul 21, 2024
1 parent 32f150b commit 142deef
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion openssl/src/bio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ impl MemBio {
unsafe {
let mut ptr = ptr::null_mut();
let len = ffi::BIO_get_mem_data(self.0, &mut ptr);
slice::from_raw_parts(ptr as *const _ as *const _, len as usize)
if len == 0 {
&[]
} else {
slice::from_raw_parts(ptr as *const _ as *const _, len as usize)
}
}
}

Expand All @@ -83,3 +87,14 @@ cfg_if! {
}
}
}

#[cfg(test)]
mod tests {
use super::MemBio;

#[test]
fn test_mem_bio_get_buf_empty() {
let b = MemBio::new().unwrap();
assert_eq!(b.get_buf(), &[]);
}
}

0 comments on commit 142deef

Please sign in to comment.