diff options
author | Neal H. Walfield <neal@pep.foundation> | 2020-03-28 11:22:41 +0100 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2020-03-28 11:51:24 +0100 |
commit | 9f346322a454a8c58bff8d5265adba47a57bf498 (patch) | |
tree | 36506db1f4741b3d5071bbd4f162a664485527f8 | |
parent | 54e7c8b3a0e66e8c34653efbaaf0967abbd7e9aa (diff) |
openpgp: Fix determining whether content was read.
- Content is read if `amount > 0`, not if `data.len() > 0`.
- Because `BufferedReader::data` can return more than the amount
requested, we might mistakenly set `content_was_read`.
-rw-r--r-- | openpgp/src/parse.rs | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index 30006be1..36d88a31 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -3899,10 +3899,9 @@ impl<'a> BufferedReader<Cookie> for PacketParser<'a> { if let Some(mut body_hash) = self.body_hash.take() { let data = self.data_hard(amount) .expect("It is an error to consume more than data returns"); - let read_something = data.len() > 0; body_hash.update(&data[..amount]); self.body_hash = Some(body_hash); - self.content_was_read |= read_something; + self.content_was_read |= amount > 0; } else { panic!("body_hash is None"); } @@ -3910,14 +3909,14 @@ impl<'a> BufferedReader<Cookie> for PacketParser<'a> { self.reader.consume(amount) } - fn data_consume(&mut self, amount: usize) -> io::Result<&[u8]> { + fn data_consume(&mut self, mut amount: usize) -> io::Result<&[u8]> { // This is awkward. Juggle mutable references around. if let Some(mut body_hash) = self.body_hash.take() { let data = self.data(amount)?; - let read_something = data.len() > 0; + amount = cmp::min(data.len(), amount); body_hash.update(&data[..amount]); self.body_hash = Some(body_hash); - self.content_was_read |= read_something; + self.content_was_read |= amount > 0; } else { panic!("body_hash is None"); } @@ -3929,10 +3928,9 @@ impl<'a> BufferedReader<Cookie> for PacketParser<'a> { // This is awkward. Juggle mutable references around. if let Some(mut body_hash) = self.body_hash.take() { let data = self.data_hard(amount)?; - let read_something = data.len() > 0; body_hash.update(&data[..amount]); self.body_hash = Some(body_hash); - self.content_was_read |= read_something; + self.content_was_read |= amount > 0; } else { panic!("body_hash is None"); } |