Skip to content

Commit

Permalink
rust/protover: reject extra commas
Browse files Browse the repository at this point in the history
The C implementation had gotten this wrong too, in a slightly different way.

Introduced in 5af03c1.

Fixes #27197; bugfix on 0.3.3.3-alpha.
  • Loading branch information
cypherpunks committed Aug 18, 2018
1 parent 87aacbf commit a1cc956
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions changes/bug27197
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
o Minor bugfixes (protover, rust):
- Reject extra commas in version string. Fixes bug 27197; bugfix on
0.3.3.3-alpha.
22 changes: 14 additions & 8 deletions src/rust/protover/protoset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,16 @@ impl FromStr for 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(ProtoSet::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)?;
Expand All @@ -367,11 +370,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[..])
}
}
Expand Down Expand Up @@ -536,6 +535,13 @@ mod test {
assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("-1"));
}

#[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"));
Expand Down

0 comments on commit a1cc956

Please sign in to comment.