diff options
author | Neal H. Walfield <neal@pep.foundation> | 2019-10-30 13:17:32 +0100 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2019-10-30 13:26:23 +0100 |
commit | 2d9acafcc417257934c6cfc79f32cea6a0efcff1 (patch) | |
tree | 3689c254a63bb4e62a61e49bd0cf7e861237e12f /openpgp/src/parse/packet_parser_builder.rs | |
parent | 4a7eb553c489f9709928e64f580b5888885d88c3 (diff) |
buffered-reader: Fix Generic::data_helper.
- The `Generic::data_helper` was not sufficiently careful when
dealing with errors. In particular:
- If `Generic::data_hard(1)` was called, and an error occured, and
nothing was buffered or read, `Generic::data_helper` would
return the empty string instead of the error.
- If `Generic::data(n)` was called, and an error occured, but some
data (< n bytes) was buffered, `Generic::data_helper` would
return the error instead of the data that was read.
- Fix these bugs.
- Also, simplify the code and don't save whether we hit EOF or the
error (which we can only return once, anyway). Instead, rely on
the underlying reader to return EOF or the error again.
- Fixes #174.
Diffstat (limited to 'openpgp/src/parse/packet_parser_builder.rs')
-rw-r--r-- | openpgp/src/parse/packet_parser_builder.rs | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/openpgp/src/parse/packet_parser_builder.rs b/openpgp/src/parse/packet_parser_builder.rs index b331801a..e8e90cfd 100644 --- a/openpgp/src/parse/packet_parser_builder.rs +++ b/openpgp/src/parse/packet_parser_builder.rs @@ -170,22 +170,26 @@ impl<'a> PacketParserBuilder<'a> { Dearmor::Enabled(mode) => Some(mode), Dearmor::Disabled => None, Dearmor::Auto(mode) => { - let mut reader = buffered_reader::Dup::with_cookie( - self.bio, Cookie::default()); - let header = packet::Header::parse(&mut reader); - self.bio = Box::new(reader).into_inner().unwrap(); - if let Ok(header) = header { - if let Err(_) = header.valid(false) { - // Invalid header: better try an ASCII armor - // decoder. - Some(mode) + if self.bio.eof() { + None + } else { + let mut reader = buffered_reader::Dup::with_cookie( + self.bio, Cookie::default()); + let header = packet::Header::parse(&mut reader); + self.bio = Box::new(reader).into_inner().unwrap(); + if let Ok(header) = header { + if let Err(_) = header.valid(false) { + // Invalid header: better try an ASCII armor + // decoder. + Some(mode) + } else { + None + } } else { - None + // Failed to parse the header: better try an ASCII + // armor decoder. + Some(mode) } - } else { - // Failed to parse the header: better try an ASCII - // armor decoder. - Some(mode) } } }; @@ -237,13 +241,7 @@ mod tests { let ppr = PacketParserBuilder::from_bytes(msg).unwrap() .dearmor(Dearmor::Enabled(Default::default())) .finalize(); - // XXX: If the dearmorer doesn't find a header and has no - // data, then it should return an error. Fix this when - // https://gitlab.com/sequoia-pgp/sequoia/issues/174 is - // resolved. - // - // assert_match!(Err(_) = ppr); - assert_match!(Ok(PacketParserResult::EOF(ref _pp)) = ppr); + assert_match!(Err(_) = ppr); // ASCII armor encoded data. let msg = crate::tests::message("a-cypherpunks-manifesto.txt.ed25519.sig"); |