diff options
author | cypherpunks <cypherpunks@torproject.org> | 2018-08-18 13:41:19 +0000 |
---|---|---|
committer | cypherpunks <cypherpunks@torproject.org> | 2018-09-15 23:19:31 +0000 |
commit | 1ed5e009cb09b1e0b348f6c8420a0d4dae746ec9 (patch) | |
tree | 6dcc93b4e02f69727eebb607fa72a5dcf81f7a04 /src/rust | |
parent | a546e07600151be275e6134407c2bcb833a3dd97 (diff) | |
download | tor-1ed5e009cb09b1e0b348f6c8420a0d4dae746ec9.tar.gz tor-1ed5e009cb09b1e0b348f6c8420a0d4dae746ec9.zip |
rust/protover: reject extra commas
The C implementation had gotten this wrong too, in a slightly different way.
Introduced in 5af03c1ef3c4718b79abb1638f5a8c275129530a.
Fixes #27197; bugfix on 0.3.3.3-alpha.
Diffstat (limited to 'src/rust')
-rw-r--r-- | src/rust/protover/protoset.rs | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/src/rust/protover/protoset.rs b/src/rust/protover/protoset.rs index b99e1a99f7..7576324c92 100644 --- a/src/rust/protover/protoset.rs +++ b/src/rust/protover/protoset.rs @@ -329,23 +329,25 @@ impl FromStr for ProtoSet { /// assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("3-")); /// assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("1-,4")); /// - /// // Things which would get parsed into an _empty_ `ProtoSet` are, - /// // however, legal, and result in an empty `ProtoSet`: + /// // An empty string is, however, legal, and results in an + /// // empty `ProtoSet`: /// assert_eq!(Ok(ProtoSet::default()), ProtoSet::from_str("")); - /// assert_eq!(Ok(ProtoSet::default()), ProtoSet::from_str(",,,")); /// # /// # Ok(protoset) /// # } /// # fn main() { do_test(); } // wrap the test so we can use the ? operator /// ``` fn from_str(version_string: &str) -> Result<Self, Self::Err> { + // If we were passed in an empty string, then return an empty ProtoSet. + if version_string.is_empty() { + return Ok(Self::default()); + } + let mut pairs: Vec<(Version, Version)> = Vec::new(); let pieces: ::std::str::Split<char> = version_string.split(','); for p in pieces { - if p.is_empty() { - continue; - } else if p.contains('-') { + if p.contains('-') { let mut pair = p.splitn(2, '-'); let low = pair.next().ok_or(ProtoverError::Unparseable)?; @@ -367,11 +369,7 @@ impl FromStr for ProtoSet { pairs.push((v, v)); } } - // If we were passed in an empty string, or - // simply a comma, or a pile of commas, then return an empty ProtoSet. - if pairs.len() == 0 { - return Ok(ProtoSet::default()); - } + ProtoSet::from_slice(&pairs[..]) } } @@ -536,6 +534,13 @@ mod test { } #[test] + fn test_versions_from_str_commas() { + assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str(",")); + assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("1,,2")); + assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("1,2,")); + } + + #[test] fn test_versions_from_str_hyphens() { assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("--1")); assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("-1-2")); |