diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-12-14 12:54:16 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-12-14 12:54:16 +0100 |
commit | fcd9a47a92e85fd87f902703411db0d0651895d1 (patch) | |
tree | ff040a4edabcee4377a39a99bf28355a76a2abdc /openpgp/src/parse.rs | |
parent | 55c668a4028bb703a25d652b36ed9382c8c36991 (diff) |
openpgp: Fix crash in the packet parser.
- If no data has been read, that may indicate an error. In this
case, even requesting no data may fail.
Diffstat (limited to 'openpgp/src/parse.rs')
-rw-r--r-- | openpgp/src/parse.rs | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index 968e19b6..61ee0c2a 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -514,8 +514,10 @@ impl<'a, T: 'a + BufferedReader<Cookie>> PacketHeaderParser<T> { Box::new(self.reader).into_inner().unwrap() }; - // We know the data has been read, so this cannot fail. - reader.data_consume_hard(total_out).unwrap(); + if total_out > 0 { + // We know the data has been read, so this cannot fail. + reader.data_consume_hard(total_out).unwrap(); + } Ok(PacketParser { header: self.header, @@ -6057,4 +6059,25 @@ mod test { let _ = PacketParser::from_bytes(&data); Ok(()) } + + /// Tests for a panic in the packet parser. + #[test] + fn packet_parser_on_mangled_cert() -> Result<()> { + // The armored input cert is mangled. Currently, Sequoia + // doesn't grok the mangled armor, but it should not panic. + let mut ppr = match PacketParser::from_bytes( + crate::tests::key("bobs-cert-badly-mangled.asc")) { + Ok(ppr) => ppr, + Err(_) => return Ok(()), + }; + while let PacketParserResult::Some(pp) = ppr { + dbg!(&pp.packet); + if let Ok((_, tmp)) = pp.recurse() { + ppr = tmp; + } else { + break; + } + } + Ok(()) + } } |