mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
option_parser: Reject empty keys in Tuple
We reject empty unquoted `Tuple` keys in inputs such as "@42", as this is malformed input. Signed-off-by: Pascal Scholz <pascal.scholz@cyberus-technology.de> On-behalf-of: SAP pascal.scholz@sap.com
This commit is contained in:
committed by
Rob Bradford
parent
4c2328b723
commit
d054a38600
@@ -476,6 +476,8 @@ pub enum TupleError {
|
|||||||
InvalidIntegerList(#[source] IntegerListParseError),
|
InvalidIntegerList(#[source] IntegerListParseError),
|
||||||
#[error("Invalid integer")]
|
#[error("Invalid integer")]
|
||||||
InvalidInteger(#[source] ParseIntError),
|
InvalidInteger(#[source] ParseIntError),
|
||||||
|
#[error("Empty key in tuple: {0}")]
|
||||||
|
EmptyKey(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A tuple consisting of a `key@value` pair parsed from a string.
|
/// A tuple consisting of a `key@value` pair parsed from a string.
|
||||||
@@ -503,7 +505,11 @@ impl<S: Parseable, T: TupleValue> Parseable for Tuple<S, T> {
|
|||||||
if last_idx != 0 {
|
if last_idx != 0 {
|
||||||
return Err(TupleError::InvalidValue((*tuple).to_string()));
|
return Err(TupleError::InvalidValue((*tuple).to_string()));
|
||||||
}
|
}
|
||||||
first_val = Some(&tuple[last_idx..idx]);
|
first_val = if tuple[last_idx..idx].is_empty() {
|
||||||
|
return Err(TupleError::EmptyKey((*tuple).to_string()));
|
||||||
|
} else {
|
||||||
|
Some(&tuple[last_idx..idx])
|
||||||
|
};
|
||||||
last_idx = idx + 1;
|
last_idx = idx + 1;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@@ -888,6 +894,17 @@ mod unit_tests {
|
|||||||
Tuple::<String, u64>::from_str("foo42").unwrap_err();
|
Tuple::<String, u64>::from_str("foo42").unwrap_err();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_missing_key() {
|
||||||
|
let expected_value = "@42";
|
||||||
|
let e = Tuple::<String, u64>::from_str("@42").unwrap_err();
|
||||||
|
assert!(
|
||||||
|
matches!(e, TupleError::EmptyKey(ref s) if s == expected_value),
|
||||||
|
"Expected \"{:?}\"; got \"{e:?}\"",
|
||||||
|
TupleError::EmptyKey(expected_value.to_string()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_split_commas_unbalanced_bracket() {
|
fn test_split_commas_unbalanced_bracket() {
|
||||||
split_commas("[a,b").unwrap_err();
|
split_commas("[a,b").unwrap_err();
|
||||||
|
|||||||
Reference in New Issue
Block a user