Skip to content

Commit

Permalink
Don't lie about buffer initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Dec 5, 2023
1 parent ebbd128 commit 39e7b5d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 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
17 changes: 5 additions & 12 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 @@ -228,18 +227,12 @@ where
ctx: &mut Context<'_>,
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 and guarantees that the first nread
// bytes are initialized.
self.with_context(ctx, |s| unsafe {
match cvt(s.read_uninit(buf.unfilled_mut()))? {
Poll::Ready(nread) => {
unsafe {
buf.assume_init(nread);
}
buf.assume_init(nread);
buf.advance(nread);
Poll::Ready(Ok(()))
}
Expand Down

0 comments on commit 39e7b5d

Please sign in to comment.