1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +00:00

od: do not panic on empty address radix

This commit is contained in:
Andrew Liebenow 2024-09-01 07:59:19 -05:00
parent dfe3e38063
commit 04cd36f022
2 changed files with 31 additions and 16 deletions

View file

@ -171,23 +171,30 @@ impl OdOptions {
None => Radix::Octal, None => Radix::Octal,
Some(s) => { Some(s) => {
// Other implementations of od only check the first character of this argument's value. // Other implementations of od only check the first character of this argument's value.
// This means executing "od -Anone" is equivalent to "od -An". // This means executing `od -Anone` is equivalent to executing `od -An`.
// Existing users of od rely on this behavior: // Existing users of od rely on this behavior:
// https://github.com/landley/toybox/blob/d50372cad35d5dd12e6391c3c7c901a96122dc67/scripts/make.sh#L239 // https://github.com/landley/toybox/blob/d50372cad35d5dd12e6391c3c7c901a96122dc67/scripts/make.sh#L239
// https://github.com/google/jsonnet/blob/913281d203578bb394995bacc792f2576371e06c/Makefile#L212 // https://github.com/google/jsonnet/blob/913281d203578bb394995bacc792f2576371e06c/Makefile#L212
let st = s.as_bytes(); let st = s.as_bytes();
let radix: char = *(st.first().expect("should be caught by clap")) as char; if let Some(u) = st.first() {
match radix { match *u {
'd' => Radix::Decimal, b'o' => Radix::Octal,
'x' => Radix::Hexadecimal, b'd' => Radix::Decimal,
'o' => Radix::Octal, b'x' => Radix::Hexadecimal,
'n' => Radix::NoPrefix, b'n' => Radix::NoPrefix,
_ => { _ => {
return Err(USimpleError::new( return Err(USimpleError::new(
1, 1,
"Radix must be one of [d, o, n, x]".to_string(), "Radix must be one of [o, d, x, n]".to_string(),
)) ));
}
} }
} else {
// Return an error instead of panicking when `od -A ''` is executed.
return Err(USimpleError::new(
1,
"Radix cannot be empty, and must be one of [o, d, x, n]".to_string(),
));
} }
} }
}; };

View file

@ -579,6 +579,15 @@ fn test_invalid_offset() {
new_ucmd!().arg("-Ab").fails(); new_ucmd!().arg("-Ab").fails();
} }
#[test]
fn test_empty_offset() {
new_ucmd!()
.arg("-A")
.arg("")
.fails()
.stderr_only("od: Radix cannot be empty, and must be one of [o, d, x, n]\n");
}
#[test] #[test]
fn test_offset_compatibility() { fn test_offset_compatibility() {
let input = [0u8; 4]; let input = [0u8; 4];
@ -586,10 +595,9 @@ fn test_offset_compatibility() {
new_ucmd!() new_ucmd!()
.arg("-Anone") .arg("-Anone")
.run_piped_stdin(input) .pipe_in(input)
.no_stderr() .succeeds()
.success() .stdout_only(expected_output);
.stdout_is(expected_output);
} }
#[test] #[test]