Skip to content

Commit

Permalink
Merge pull request #47 from tokio-rs/maybe-uninit
Browse files Browse the repository at this point in the history
Don't lie about buffer initialization
  • Loading branch information
sfackler authored Dec 9, 2023
2 parents ebbd128 + 3209032 commit 8b0b96d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ An implementation of SSL streams for Tokio backed by OpenSSL

[dependencies]
futures-util = { version = "0.3", default-features = false }
openssl = "0.10.32"
openssl = "0.10.61"
openssl-sys = "0.9"
tokio = "1.0"

Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::unusual_byte_groupings)]
use std::env;

fn main() {
Expand Down
15 changes: 4 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use openssl::ssl::{self, ErrorCode, ShutdownResult, Ssl, SslRef};
use std::fmt;
use std::io::{self, Read, Write};
use std::pin::Pin;
use std::slice;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

Expand Down Expand Up @@ -229,17 +228,11 @@ where
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
self.with_context(ctx, |s| {
// This isn't really "proper", but rust-openssl doesn't currently expose a suitable interface even though
// OpenSSL itself doesn't require the buffer to be initialized. So this is good enough for now.
let slice = unsafe {
let buf = buf.unfilled_mut();
slice::from_raw_parts_mut(buf.as_mut_ptr().cast::<u8>(), buf.len())
};
match cvt(s.read(slice))? {
// SAFETY: read_uninit does not de-initialize the buffer.
match cvt(s.read_uninit(unsafe { buf.unfilled_mut() }))? {
Poll::Ready(nread) => {
unsafe {
buf.assume_init(nread);
}
// SAFETY: read_uninit guarantees that nread bytes have been initialized.
unsafe { buf.assume_init(nread) };
buf.advance(nread);
Poll::Ready(Ok(()))
}
Expand Down

0 comments on commit 8b0b96d

Please sign in to comment.