Skip to content

Commit

Permalink
Fix blocking when reading into empty buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 4, 2023
1 parent c0b1451 commit 1a69d21
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 0.16.3 - ?

- Don't block when reading into an empty buffer

## 0.16.2 - 2023-11-02

- Re-add `impl_trait_projections` to support older nightly rustc versions
Expand Down
6 changes: 6 additions & 0 deletions src/asynch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ where

/// Read and decrypt data filling the provided slice.
pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize, TlsError> {
if buf.is_empty() {
return Ok(0);
}
let mut buffer = self.read_buffered().await?;

let len = buffer.pop_into(buf);
Expand Down Expand Up @@ -460,6 +463,9 @@ where
State: SplitState,
{
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
if buf.is_empty() {
return Ok(0);
}
let mut buffer = self.read_buffered().await?;

let len = buffer.pop_into(buf);
Expand Down
6 changes: 6 additions & 0 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ where

/// Read and decrypt data filling the provided slice.
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize, TlsError> {
if buf.is_empty() {
return Ok(0);
}
let mut buffer = self.read_buffered()?;

let len = buffer.pop_into(buf);
Expand Down Expand Up @@ -450,6 +453,9 @@ where
State: SplitState,
{
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
if buf.is_empty() {
return Ok(0);
}
let mut buffer = self.read_buffered()?;

let len = buffer.pop_into(buf);
Expand Down
10 changes: 10 additions & 0 deletions tests/client_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ async fn test_ping() {
assert_eq!(b"ping", &rx_buf[..sz]);
log::info!("Read {} bytes: {:?}", sz, &rx_buf[..sz]);

// Test that embedded-tls doesn't block if the buffer is empty.
let mut rx_buf = [0; 0];
let sz = tls.read(&mut rx_buf).await.expect("error reading data");
assert_eq!(sz, 0);

tls.close()
.await
.map_err(|(_, e)| e)
Expand Down Expand Up @@ -304,6 +309,11 @@ fn test_blocking_ping() {
assert_eq!(b"ping", &rx_buf[..sz]);
log::info!("Read {} bytes: {:?}", sz, &rx_buf[..sz]);

// Test that embedded-tls doesn't block if the buffer is empty.
let mut rx_buf = [0; 0];
let sz = tls.read(&mut rx_buf).expect("error reading data");
assert_eq!(sz, 0);

tls.close()
.map_err(|(_, e)| e)
.expect("error closing session");
Expand Down

0 comments on commit 1a69d21

Please sign in to comment.