Skip to content

Commit

Permalink
Merge pull request #318 from cgwalters/invalid-name-slash
Browse files Browse the repository at this point in the history
Reject an empty name
  • Loading branch information
cgwalters committed Aug 20, 2024
2 parents 69508cb + 81f1d1b commit 7aae7ab
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion libcomposefs/lcfs-writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,12 @@ int lcfs_node_add_child(struct lcfs_node_s *parent, struct lcfs_node_s *child,
return -1;
}

if (strlen(name) > LCFS_MAX_NAME_LENGTH) {
const size_t namelen = strlen(name);
if (namelen == 0) {
errno = EINVAL;
return -1;
}
if (namelen > LCFS_MAX_NAME_LENGTH) {
errno = ENAMETOOLONG;
return -1;
}
Expand Down
13 changes: 12 additions & 1 deletion rust/composefs/src/dumpfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,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 @@ -433,6 +439,11 @@ mod tests {
}
}

#[test]
fn test_unescape_path() {
assert!(unescape_to_path("").is_err());
}

#[test]
fn test_parse() {
const CONTENT: &str = include_str!("../../../tests/assets/special.dump");
Expand Down
2 changes: 2 additions & 0 deletions tests/assets/should-fail-empty-name.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/ 4096 40555 2 0 0 0 1633950376.0 - - -
// 4096 40555 2 0 0 0 1633950376.0 - - -
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test_assets_should_fail = [
'should-fail-long-xattr-key.dump',
'should-fail-long-xattr-value.dump',
'should-fail-no-ftype.dump',
'should-fail-empty-name.dump',
]

test_assets = test_assets_small + [
Expand Down
3 changes: 3 additions & 0 deletions tools/mkcomposefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ static char *tree_from_dump_line(dump_info *info, const char *line, size_t line_
fields[FIELD_PATH].data, fields[FIELD_PATH].len, NULL, &err);
if (path == NULL && err)
return err;
if (!*path) {
return make_error("Invalid empty path");
}

bool is_hardlink = false;
/* First char in mode is @ if hardlink */
Expand Down

0 comments on commit 7aae7ab

Please sign in to comment.