Skip to content

Commit

Permalink
fix(time-style): cryptic panic with invalid --time-syle format string
Browse files Browse the repository at this point in the history
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: #1240
  • Loading branch information
zea64 committed Dec 29, 2024
1 parent 8d17115 commit a905b8b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/options/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::output::table::{
use crate::output::time::TimeFormat;
use crate::output::{details, grid, Mode, TerminalWidth, View};

use chrono::format::{Item, StrftimeItems};

impl View {
pub fn deduce<V: Vars>(matches: &MatchedFlags<'_>, vars: &V) -> Result<Self, OptionsError> {
let mode = Mode::deduce(matches, vars)?;
Expand Down Expand Up @@ -350,12 +352,17 @@ impl TimeFormat {
// - there is nothing after `+`
// line 1 will be empty when:
// - `+` is followed immediately by `\n`
let empty_non_recent_format_msg = "Custom timestamp format is empty, \
please supply a chrono format string after the plus sign.";
let non_recent = lines.next().expect(empty_non_recent_format_msg);
let invalid_non_recent_format_msg = "Custom timestamp format is invalid, \
please supply a valid chrono format string after the plus sign.";
let non_recent = lines.next().expect(invalid_non_recent_format_msg);
let non_recent = if non_recent.is_empty() {
panic!("{}", empty_non_recent_format_msg)
panic!("{}", invalid_non_recent_format_msg)
} else {
StrftimeItems::new(non_recent).for_each(|item| {
if matches!(item, Item::Error) {
panic!("{}", invalid_non_recent_format_msg)
}
});
non_recent.to_owned()
};

Expand All @@ -364,12 +371,18 @@ impl TimeFormat {
// - there is nothing after the first `\n`
// line 2 will be empty when:
// - there exist at least 2 `\n`, and no content between the 1st and 2nd `\n`
let empty_recent_format_msg = "Custom timestamp format for recent files is empty, \
please supply a chrono format string at the second line.";
let invalid_recent_format_msg =
"Custom timestamp format for recent files is invalid, \
please supply a valid chrono format string at the second line.";
let recent = lines.next().map(|rec| {
if rec.is_empty() {
panic!("{}", empty_recent_format_msg)
panic!("{}", invalid_recent_format_msg)
} else {
StrftimeItems::new(rec).for_each(|item| {
if matches!(item, Item::Error) {
panic!("{}", invalid_recent_format_msg)
}
});
rec.to_owned()
}
});
Expand Down

0 comments on commit a905b8b

Please sign in to comment.