Skip to content

find: -newerXt with a non-ASCII-digit year aborts instead of erroring #759

Closed

Description

@leeewee

The -newerXt time options (-newermt, -newerat, -newerct, -newerBt) parse the year field of their date argument with .parse::<i32>().unwrap() (src/find/matchers/mod.rs:384). The year is captured by a regex whose \d class matches Unicode decimal digits (the regex crate defaults to Unicode mode, so \d == \p{Nd}), but str::parse::<i32>() accepts only ASCII digits. So a year written with four non-ASCII decimal digits (Arabic-Indic, Devanagari, fullwidth, …) passes the regex and then panics in parse, aborting the process with exit 101.

$ find . -newermt 'jan 01, ٠٠٠٠'
thread 'main' panicked at src/find/matchers/mod.rs:384:56:
called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ echo $?
101

Any four non-ASCII decimal digits in the year field trigger it — the digit system is irrelevant:

$ find . -newermt 'jan 01, 0001'   # fullwidth digits
thread 'main' panicked at src/find/matchers/mod.rs:384:56: ...
$ find . -newermt 'jan 01, ००१२'      # Devanagari digits
thread 'main' panicked at src/find/matchers/mod.rs:384:56: ...

GNU findutils find rejects the same argument cleanly:

$ /usr/bin/find . -newermt 'jan 01, ٠٠٠٠'
/usr/bin/find: I cannot figure out how to interpret ‘jan 01, ٠٠٠٠’ as a date or time
$ echo $?
1

Root cause

parse_date_str_to_timestamps (src/find/matchers/mod.rs):

let regex_pattern =
    r"^(?P<month_day>\w{3} \d{2})?(?:, (?P<year>\d{4}))?(?: (?P<time>\d{2}:\d{2}:\d{2}))?$";
let re = Regex::new(regex_pattern);
if let Some(captures) = re.ok()?.captures(date_str) {
    ...
    let year = captures
        .get(2)
        .map_or(now.year(), |m| m.as_str().parse().unwrap());   // <-- mod.rs:384: panics
    ...
    let datetime = NaiveDateTime::parse_from_str(&date_time_str, "%b %d, %Y %H:%M:%S").ok()?;
    ...
}

\d{4} matches four Unicode decimal digits, but m.as_str().parse::<i32>() (the type is inferred from the now.year(): i32 branch) rejects non-ASCII digits with ParseIntError { kind: InvalidDigit }, which the .unwrap() turns into a panic.

Found by our static analysis tooling.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions