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

Merge pull request #6675 from andrewliebenow/od-do-not-panic-on-empty-address-radix

od: do not panic on empty address radix
This commit is contained in:
Daniel Hofstetter 2024-09-01 16:14:10 +02:00 committed by GitHub
commit 2e0cd7bd37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 16 deletions

View file

@ -171,24 +171,31 @@ impl OdOptions {
None => Radix::Octal,
Some(s) => {
// 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:
// https://github.com/landley/toybox/blob/d50372cad35d5dd12e6391c3c7c901a96122dc67/scripts/make.sh#L239
// https://github.com/google/jsonnet/blob/913281d203578bb394995bacc792f2576371e06c/Makefile#L212
let st = s.as_bytes();
let radix: char = *(st.first().expect("should be caught by clap")) as char;
match radix {
'd' => Radix::Decimal,
'x' => Radix::Hexadecimal,
'o' => Radix::Octal,
'n' => Radix::NoPrefix,
if let Some(u) = st.first() {
match *u {
b'o' => Radix::Octal,
b'd' => Radix::Decimal,
b'x' => Radix::Hexadecimal,
b'n' => Radix::NoPrefix,
_ => {
return Err(USimpleError::new(
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();
}
#[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]
fn test_offset_compatibility() {
let input = [0u8; 4];
@ -586,10 +595,9 @@ fn test_offset_compatibility() {
new_ucmd!()
.arg("-Anone")
.run_piped_stdin(input)
.no_stderr()
.success()
.stdout_is(expected_output);
.pipe_in(input)
.succeeds()
.stdout_only(expected_output);
}
#[test]