aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/ssl/mod.rs
diff options
context:
space:
mode:
authorAlex Crichton <[email protected]>2016-12-20 14:04:10 -0800
committerAlex Crichton <[email protected]>2016-12-20 15:52:18 -0800
commit8e01f8d2502098497e642ee477d926a99ee619a8 (patch)
tree6db689d730ca6573ace1f7b876bfcf3d34cda33b /openssl/src/ssl/mod.rs
parentMerge branch 'master' of github.com:sfackler/rust-openssl (diff)
downloadrust-openssl-8e01f8d2502098497e642ee477d926a99ee619a8.tar.xz
rust-openssl-8e01f8d2502098497e642ee477d926a99ee619a8.zip
Handle zero-length reads/writes
This commit adds some short-circuits for zero-length reads/writes to `SslStream`. Because OpenSSL returns 0 on error, then we could mistakenly confuse a 0-length success as an actual error, so we avoid writing or reading 0 bytes by returning quickly with a success.
Diffstat (limited to 'openssl/src/ssl/mod.rs')
-rw-r--r--openssl/src/ssl/mod.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs
index 6e0c92c3..47c83453 100644
--- a/openssl/src/ssl/mod.rs
+++ b/openssl/src/ssl/mod.rs
@@ -1506,6 +1506,15 @@ impl<S: Read + Write> SslStream<S> {
/// This is particularly useful with a nonblocking socket, where the error
/// value will identify if OpenSSL is waiting on read or write readiness.
pub fn ssl_read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
+ // The intepretation of the return code here is a little odd with a
+ // zero-length write. OpenSSL will likely correctly report back to us
+ // that it read zero bytes, but zero is also the sentinel for "error".
+ // To avoid that confusion short-circuit that logic and return quickly
+ // if `buf` has a length of zero.
+ if buf.len() == 0 {
+ return Ok(0)
+ }
+
let ret = self.ssl.read(buf);
if ret > 0 {
Ok(ret as usize)
@@ -1523,6 +1532,11 @@ impl<S: Read + Write> SslStream<S> {
/// This is particularly useful with a nonblocking socket, where the error
/// value will identify if OpenSSL is waiting on read or write readiness.
pub fn ssl_write(&mut self, buf: &[u8]) -> Result<usize, Error> {
+ // See above for why we short-circuit on zero-length buffers
+ if buf.len() == 0 {
+ return Ok(0)
+ }
+
let ret = self.ssl.write(buf);
if ret > 0 {
Ok(ret as usize)