Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/uu/stat/src/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,19 +943,22 @@ impl Stater {
'"' => Token::Byte(b'"'), // Double quote
'0'..='7' => {
// Parse octal escape sequence (up to 3 digits)
let mut value = 0u8;
// Accumulate in a wider type: three octal digits can reach 511,
// and only the low byte is kept, which is what GNU prints for
// an out-of-range escape such as `\400`.
let mut value = 0u32;
let mut count = 0;
while *i < bound && count < 3 {
if let Some(digit) = chars[*i].to_digit(8) {
value = value * 8 + digit as u8;
value = value * 8 + digit;
*i += 1;
count += 1;
} else {
break;
}
}
*i -= 1; // Adjust index to account for the outer loop increment
Token::Byte(value)
Token::Byte(value as u8)
}
'x' => {
// Parse hexadecimal escape sequence (\xNN format)
Expand Down
11 changes: 11 additions & 0 deletions tests/by-util/test_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,17 @@ fn test_printf_octal_2() {
.stdout_is_bytes(expected_stdout);
}

#[test]
fn test_printf_octal_out_of_range() {
// Octal escapes whose value exceeds 255 wrap around, as they do in GNU stat.
let ts = TestScenario::new(util_name!());
let expected_stdout = vec![0x00, 0xFF]; // \400 -> 256 & 0xFF, \777 -> 511 & 0xFF
ts.ucmd()
.args(&["--printf=\\400\\777", "."])
.succeeds()
.stdout_is_bytes(expected_stdout);
}

#[test]
fn test_printf_incomplete_hex() {
let ts = TestScenario::new(util_name!());
Expand Down
Loading