Skip to content

Commit

Permalink
rust: Reject empty name
Browse files Browse the repository at this point in the history
Corresponding to the C change.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Aug 19, 2024
1 parent 500e7e3 commit 0166251
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions rust/composefs/src/dumpfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ fn unescape_to_osstr(s: &str) -> Result<Cow<OsStr>> {
/// 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<Cow<Path>> {
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)),
};
Expand Down Expand Up @@ -255,7 +261,7 @@ impl<'p> Entry<'p> {
pub fn parse(s: &'p str) -> Result<Entry<'p>> {
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('@') {
Expand Down Expand Up @@ -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)] = &[
Expand Down

0 comments on commit 0166251

Please sign in to comment.