From 0ac1c16d62b7a1900779be513579419f6a0ec730 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Fri, 24 Jul 2020 15:49:43 +0200 Subject: openpgp: Improve PacketParserResult::as_ref, as_mut, and map. - Previously, these method withheld information in the EOF case (and in case of `map` this loss is irrecoverable). Fix this by returning a Result instead. --- autocrypt/src/lib.rs | 2 +- openpgp/src/packet_pile.rs | 2 +- openpgp/src/parse.rs | 27 ++++++++++++++------------- openpgp/src/parse/packet_pile_parser.rs | 10 +++++----- tool/src/commands/mod.rs | 2 +- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/autocrypt/src/lib.rs b/autocrypt/src/lib.rs index 06dbd696..b42e3153 100644 --- a/autocrypt/src/lib.rs +++ b/autocrypt/src/lib.rs @@ -686,7 +686,7 @@ impl<'a> AutocryptSetupMessageParser<'a> { // Recurse into the SEIP packet. let mut ppr = self.pp.recurse()?.1; - if ppr.as_ref().map(|pp| pp.recursion_depth()) != Some(1) { + if ppr.as_ref().map(|pp| pp.recursion_depth()).ok() != Some(1) { return Err( Error::MalformedMessage( "SEIP container empty, but expected a Literal Data packet" diff --git a/openpgp/src/packet_pile.rs b/openpgp/src/packet_pile.rs index bb505a96..07ca3561 100644 --- a/openpgp/src/packet_pile.rs +++ b/openpgp/src/packet_pile.rs @@ -760,7 +760,7 @@ mod test { .buffer_unread_content() .try_into().unwrap(); - while let Some(pp) = ppp.as_ref() { + while let Ok(pp) = ppp.as_ref() { eprintln!("{:?}", pp); ppp.recurse().unwrap(); } diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index 16c7ec6e..f36f634c 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -175,6 +175,7 @@ use std::str; use std::mem; use std::fmt; use std::path::Path; +use std::result::Result as StdResult; use ::buffered_reader::*; @@ -3315,20 +3316,20 @@ impl<'a> PacketParserResult<'a> { } /// Like `Option::as_ref`(). - pub fn as_ref(&self) -> Option<&PacketParser<'a>> { - if let PacketParserResult::Some(ref pp) = self { - Some(pp) - } else { - None + pub fn as_ref(&self) + -> StdResult<&PacketParser<'a>, &PacketParserEOF> { + match self { + PacketParserResult::Some(pp) => Ok(pp), + PacketParserResult::EOF(eof) => Err(eof), } } /// Like `Option::as_mut`(). - pub fn as_mut(&mut self) -> Option<&mut PacketParser<'a>> { - if let PacketParserResult::Some(ref mut pp) = self { - Some(pp) - } else { - None + pub fn as_mut(&mut self) + -> StdResult<&mut PacketParser<'a>, &mut PacketParserEOF> { + match self { + PacketParserResult::Some(pp) => Ok(pp), + PacketParserResult::EOF(eof) => Err(eof), } } @@ -3345,12 +3346,12 @@ impl<'a> PacketParserResult<'a> { } /// Like `Option::map`(). - pub fn map(self, f: F) -> Option + pub fn map(self, f: F) -> StdResult where F: FnOnce(PacketParser<'a>) -> U { match self { - PacketParserResult::Some(x) => Some(f(x)), - PacketParserResult::EOF(_) => None, + PacketParserResult::Some(x) => Ok(f(x)), + PacketParserResult::EOF(e) => Err(e), } } } diff --git a/openpgp/src/parse/packet_pile_parser.rs b/openpgp/src/parse/packet_pile_parser.rs index 2540026a..78483b82 100644 --- a/openpgp/src/parse/packet_pile_parser.rs +++ b/openpgp/src/parse/packet_pile_parser.rs @@ -54,7 +54,7 @@ use buffered_reader::BufferedReader; /// # include_bytes!("../../tests/data/keys/public-key.gpg"); /// # let mut n = 0; /// let mut ppp = PacketPileParser::from_bytes(message_data)?; -/// while let Some(pp) = ppp.as_ref() { +/// while let Ok(pp) = ppp.as_ref() { /// eprintln!("{:?}", pp); /// ppp.recurse()?; /// # n += 1; @@ -198,7 +198,7 @@ impl<'a> PacketPileParser<'a> { /// let message_data: &[u8] = // ... /// # include_bytes!("../../tests/data/messages/compressed-data-algo-0.pgp"); /// let mut ppp = PacketPileParser::from_bytes(message_data)?; - /// while let Some(pp) = ppp.as_ref() { + /// while let Ok(pp) = ppp.as_ref() { /// // Do something interesting with `pp` here. /// /// // Start parsing the next packet, recursing. @@ -251,7 +251,7 @@ impl<'a> PacketPileParser<'a> { /// let message_data: &[u8] = // ... /// # include_bytes!("../../tests/data/messages/compressed-data-algo-0.pgp"); /// let mut ppp = PacketPileParser::from_bytes(message_data)?; - /// while let Some(pp) = ppp.as_ref() { + /// while let Ok(pp) = ppp.as_ref() { /// // Do something interesting with `pp` here. /// /// // Start parsing the next packet. @@ -296,7 +296,7 @@ impl<'a> PacketPileParser<'a> { /// let message_data: &[u8] = // ... /// # include_bytes!("../../tests/data/messages/compressed-data-algo-0.pgp"); /// let mut ppp = PacketPileParser::from_bytes(message_data)?; - /// while let Some(pp) = ppp.as_ref() { + /// while let Ok(pp) = ppp.as_ref() { /// match pp.packet { /// Packet::CompressedData(_) => /// assert_eq!(ppp.recursion_depth(), Some(0)), @@ -424,7 +424,7 @@ fn message_parser_reader_interface() { let mut ppp = PacketPileParser::from_bytes( crate::tests::message("compressed-data-algo-1.gpg")).unwrap(); let mut count = 0; - while let Some(pp) = ppp.as_mut() { + while let Ok(pp) = ppp.as_mut() { if let Packet::Literal(_) = pp.packet { assert_eq!(count, 1); // The *second* packet. diff --git a/tool/src/commands/mod.rs b/tool/src/commands/mod.rs index 32239d8d..04e84987 100644 --- a/tool/src/commands/mod.rs +++ b/tool/src/commands/mod.rs @@ -428,7 +428,7 @@ pub fn split(input: &mut dyn io::Read, prefix: &str) let old_depth = Some(pp.recursion_depth()); ppr = pp.recurse()?.1; - let new_depth = ppr.as_ref().map(|pp| pp.recursion_depth()); + let new_depth = ppr.as_ref().map(|pp| pp.recursion_depth()).ok(); // Update pos. match old_depth.cmp(&new_depth) { -- cgit v1.2.3