Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/uu/touch/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,10 @@ fn parse_date(ref_zoned: Zoned, s: &str) -> Result<FileTime, TouchError> {
/// - 68 and before is interpreted as 20xx
/// - 69 and after is interpreted as 19xx
fn prepend_century(s: &str) -> UResult<String> {
let first_two_digits = s[..2].parse::<u32>().map_err(|_| {
// Take the first two chars rather than byte-slicing `s[..2]`: a leading
// multibyte char would otherwise split a UTF-8 boundary and panic.
let first_two: String = s.chars().take(2).collect();
let first_two_digits = first_two.parse::<u32>().map_err(|_| {
USimpleError::new(
1,
translate!("touch-error-invalid-date-ts-format", "date" => s.quote()),
Expand Down
10 changes: 10 additions & 0 deletions tests/by-util/test_touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,16 @@ fn test_touch_invalid_date_format() {
.stderr_contains("touch: invalid date format '+1000000000000 years'");
}

#[test]
fn test_touch_invalid_timestamp_leading_multibyte_char() {
for ts in ["€123456789", "€23456789012"] {
new_ucmd!()
.args(&["-t", ts, "f"])
.fails_with_code(1)
.stderr_only(format!("touch: invalid date ts format '{ts}'\n"));
}
}

#[test]
#[cfg(not(target_os = "freebsd"))]
fn test_touch_symlink_with_no_deref() {
Expand Down
Loading