Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: panic on invalid time-style format #1240

Open
xitep opened this issue Nov 19, 2024 · 2 comments · May be fixed by #1282
Open

bug: panic on invalid time-style format #1240

xitep opened this issue Nov 19, 2024 · 2 comments · May be fixed by #1282
Labels
errors Something isn't working

Comments

@xitep
Copy link

xitep commented Nov 19, 2024

> eza --version
eza - A modern, maintained replacement for ls
v0.20.8 [+git]
https://github.com/eza-community/eza

# terminal
> foot --version
foot version: 1.18.1 +pgo +ime +graphemes -assertions

# shell
> fish --version
fish, version 3.7.1

# os
> lsb_release -a
LSB Version:	n/a
Distributor ID:	ManjaroLinux
Description:	Manjaro Linux
Release:	24.1.2
Codename:	Xahea


# now ... reproduce the panic
> eza -l --time-style '+%'
thread '<unnamed>' panicked at /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/alloc/src/string.rs:2566:14:
a Display implementation returned an error unexpectedly: Error
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fish: Job 1, 'eza -l --time-style '+%'' terminated by signal SIGABRT (Abort)

i think an error message instead of a panic, akin to eza -l --time-style invalid for example, would be more appropriate.

@xitep xitep added the errors Something isn't working label Nov 19, 2024
@rritik772
Copy link

rritik772 commented Dec 21, 2024

I have traced this bug, this comes from chronos lib. It panics on invalid format do we need error handling on our side or should I raise PR on chronos lib itself.

file: src/output/time.rs

fn custom(time: &DateTime<FixedOffset>, non_recent_fmt: &str, recent_fmt: Option<&str>) -> String {
    if let Some(recent_fmt) = recent_fmt {
        if time.year() == *CURRENT_YEAR {
            time.format(recent_fmt).to_string()
        } else {
            time.format(non_recent_fmt).to_string()
        }
    } else {
        time.format(non_recent_fmt).to_string()
    }
}

@zea64
Copy link
Contributor

zea64 commented Dec 29, 2024

DelayedFormat gets its .to_string() from std's impl <T: Display + ?Sized> ToString for T, which panics if the Display impl returns an error. DelayedFormat's Display impl is fallible. I'm not sure what they should do when given an invalid format string other than return an error, and AFAIK there's no TryToString or TryFrom<T> for String where T: Display.

However, we can just bypass .to_string() and use Display directly. It's a bit ugly, but it works:

x.to_string()
let mut string = String::new();
if write!(&mut string, "{}", x).is_err() {
    string = "Format error".to_owned();
}
string

zea64 added a commit to zea64/eza that referenced this issue Dec 29, 2024
Validate --time-style format string during option parsing so we can give
a good error message rather than let rayon handle the panic and give no
context.

Fixes: eza-community#1240
zea64 added a commit to zea64/eza that referenced this issue Dec 29, 2024
Validate --time-style format string during option parsing so we can give
a good error message rather than let rayon handle the panic and give no
context.

Fixes: eza-community#1240
zea64 added a commit to zea64/eza that referenced this issue Dec 31, 2024
Validate --time-style format string during option parsing so we can give
a good error message rather than let rayon handle the panic and give no
context.

Fixes: eza-community#1240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
errors Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants