Commit 142deef7 authored by Alex Gaynor's avatar Alex Gaynor
Browse files

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.)
parent 32f150b0
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -63,9 +63,13 @@ impl MemBio {
        unsafe {
            let mut ptr = ptr::null_mut();
            let len = ffi::BIO_get_mem_data(self.0, &mut ptr);
            if len == 0 {
                &[]
            } else {
                slice::from_raw_parts(ptr as *const _ as *const _, len as usize)
            }
        }
    }

    #[cfg(not(boringssl))]
    pub unsafe fn from_ptr(bio: *mut ffi::BIO) -> MemBio {
@@ -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(), &[]);
    }
}