summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-02-15 10:47:09 -0500
committerNick Mathewson <nickm@torproject.org>2018-02-15 19:08:52 -0500
commit5af03c1ef3c4718b79abb1638f5a8c275129530a (patch)
treec9c2d2ac0d45150a7a4ee975d44c43d57adc797a
parentb58a2febe32f96204a94fb547e14a84c8fd32651 (diff)
downloadtor-5af03c1ef3c4718b79abb1638f5a8c275129530a.tar.gz
tor-5af03c1ef3c4718b79abb1638f5a8c275129530a.zip
rust protover: match the C implementation on empty-str cases
Empty versions lists are permitted; empty keywords are not.
-rw-r--r--src/rust/protover/protover.rs15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/rust/protover/protover.rs b/src/rust/protover/protover.rs
index 64f350c6b7..e5dc69b9a0 100644
--- a/src/rust/protover/protover.rs
+++ b/src/rust/protover/protover.rs
@@ -193,10 +193,6 @@ impl Versions {
fn from_version_string(
version_string: &str,
) -> Result<Self, &'static str> {
- if version_string.is_empty() {
- return Err("version string is empty");
- }
-
let mut versions = HashSet::<Version>::new();
for piece in version_string.split(",") {
@@ -204,6 +200,8 @@ impl Versions {
for p in expand_version_range(piece)? {
versions.insert(p);
}
+ } else if piece == "" {
+ continue;
} else {
let v = u32::from_str(piece).or(
Err("invalid protocol entry"),
@@ -456,6 +454,10 @@ fn expand_version_range(range: &str) -> Result<Range<u32>, &'static str> {
return Err("protocol range value out of range");
}
+ if lower > higher {
+ return Err("protocol range is badly formed");
+ }
+
// We can use inclusive range syntax when it becomes stable.
let result = lower..higher + 1;
@@ -583,6 +585,7 @@ fn parse_protocols_from_string_with_no_validation<'a>(
let mut parts = subproto.splitn(2, "=");
let name = match parts.next() {
+ Some("") => return Err("invalid protover entry"),
Some(n) => n,
None => return Err("invalid protover entry"),
};
@@ -650,7 +653,6 @@ pub fn compute_vote(
Ok(result) => result,
Err(_) => continue,
};
-
for (protocol, versions) in this_vote {
let supported_vers: &mut HashMap<Version, usize> =
all_count.entry(protocol).or_insert(HashMap::new());
@@ -794,7 +796,6 @@ mod test {
use super::Versions;
- assert_eq!(Err("version string is empty"), Versions::from_version_string(""));
assert_eq!(Err("invalid protocol entry"), Versions::from_version_string("a,b"));
assert_eq!(Err("invalid protocol entry"), Versions::from_version_string("1,!"));
@@ -838,7 +839,7 @@ mod test {
use super::contains_only_supported_protocols;
assert_eq!(false, contains_only_supported_protocols(""));
- assert_eq!(false, contains_only_supported_protocols("Cons="));
+ assert_eq!(true, contains_only_supported_protocols("Cons="));
assert_eq!(true, contains_only_supported_protocols("Cons=1"));
assert_eq!(false, contains_only_supported_protocols("Cons=0"));
assert_eq!(false, contains_only_supported_protocols("Cons=0-1"));