From 01662514f73ce2bf92f2252d6dd36c8333e83efe Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 19 Aug 2024 14:25:29 -0400 Subject: [PATCH] rust: Reject empty name Corresponding to the C change. Signed-off-by: Colin Walters --- rust/composefs/src/dumpfile.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/rust/composefs/src/dumpfile.rs b/rust/composefs/src/dumpfile.rs index d9baf288..ca268a1f 100644 --- a/rust/composefs/src/dumpfile.rs +++ b/rust/composefs/src/dumpfile.rs @@ -163,7 +163,13 @@ fn unescape_to_osstr(s: &str) -> Result> { /// Unescape a string into a Rust `Path` which is really just an alias for a byte array, /// although there is an implicit assumption that there are no embedded `NUL` bytes. fn unescape_to_path(s: &str) -> Result> { - let r = match unescape_to_osstr(s)? { + let v = unescape_to_osstr(s).and_then(|v| { + if v.is_empty() { + anyhow::bail!("Invalid empty path"); + } + Ok(v) + })?; + let r = match v { Cow::Borrowed(v) => Cow::Borrowed(Path::new(v)), Cow::Owned(v) => Cow::Owned(PathBuf::from(v)), }; @@ -255,7 +261,7 @@ impl<'p> Entry<'p> { pub fn parse(s: &'p str) -> Result> { let mut components = s.split(' '); let mut next = |name: &str| components.next().ok_or_else(|| anyhow!("Missing {name}")); - let path = unescape_to_path(next("path")?)?; + let path = unescape_to_path((next("path")?)?; let size = u64::from_str(next("size")?)?; let modeval = next("mode")?; let (is_hardlink, mode) = if let Some((_, rest)) = modeval.split_once('@') { @@ -484,6 +490,11 @@ mod tests { Ok(()) } + #[test] + fn test_unescape_path() { + assert!(unescape_to_path("").is_err()); + } + #[test] fn test_should_fail() { const CASES: &[(&str, &str)] = &[