Skip to content

Commit

Permalink
Merge pull request #35 from cuiwc/fix-radix-prefixes
Browse files Browse the repository at this point in the history
Trim 0b/0o/0x prefixes when parsing numbers.
  • Loading branch information
mdsteele authored Jul 21, 2024
2 parents 8d73d08 + 2c411af commit 755fc74
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,4 +1169,21 @@ mod tests {
let mut archive = Archive::new(std::io::Cursor::new(data));
let _num_entries = archive.count_entries().unwrap();
}

/// Test for entries with octal literal (0o) prefix in the mode field.
#[test]
fn read_archive_with_radix_prefixed_mode() {
let input = "\
!<arch>\n\
foo.txt/ 1487552916 501 20 0o1006447 `\n\
foobar\n";
let mut archive = Archive::new(input.as_bytes());
{
let entry = archive.next_entry().unwrap().unwrap();
assert_eq!(entry.header().identifier(), "foo.txt".as_bytes());
assert_eq!(entry.header().mode(), 0o100644);
assert_eq!(entry.header().size(), 7);
}
assert!(archive.next_entry().is_none());
}
}
6 changes: 6 additions & 0 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ fn cap_mode(mode: u32) -> u32 {

fn parse_number(field_name: &str, bytes: &[u8], radix: u32) -> Result<u64> {
if let Ok(string) = str::from_utf8(bytes) {
let string = match radix {
2 => string.trim_start_matches("0b"),
8 => string.trim_start_matches("0o"),
16 => string.trim_start_matches("0x"),
_ => string,
};
if let Ok(value) = u64::from_str_radix(string.trim_end(), radix) {
return Ok(value);
}
Expand Down

0 comments on commit 755fc74

Please sign in to comment.