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

fix format panic problem #614

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Versions with only mechanical changes will be omitted from the following list.
* Fix `DurationRound` is not TZ aware (#495)
* Implement `DurationRound` for `NaiveDateTime`
* Add `DateTime::from_local()` to construct from given local date and time (#572)
* Fix the problem that format will panic when given invalid format string (#614)
* Correct build for wasm32-unknown-emscripten target (#568)

## 0.4.19
Expand Down
21 changes: 21 additions & 0 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3050,4 +3050,25 @@ mod tests {
assert_eq!(datetime_east, datetime_utc.with_timezone(&timezone_east));
assert_eq!(datetime_west, datetime_utc.with_timezone(&timezone_west));
}

#[test]
fn test_format_to_string() {
// create a naive date
let naive_date = NaiveDate::from_ymd(2000, 1, 12);
assert_eq!(format!("{}", naive_date.format("%Y %b %d")), String::from("2000 Jan 12"));
assert_eq!(format!("{}", naive_date.format("%F")), String::from("2000-01-12"));
// try to format something that the instance doesn't have
assert_eq!(format!("{}", naive_date.format("%r")), String::from(":: "));
// pass a wrong format
assert_eq!(
format!("{}", naive_date.format("%Y %{whatever} %d")),
String::from("2000 whatever} 12")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If invalid, I would imagine this would simply produce "%{whatever}", not "whatever}"

);

// create a naive time
let naive_time = NaiveTime::from_hms(12, 43, 33);
assert_eq!(format!("{}", naive_time.format("%H:%M:%S %p")), String::from("12:43:33 PM"));
// try to format something that doesn't exist
assert_eq!(format!("{}", naive_time.format("%F %H:%M:%S")), String::from("-- 12:43:33"));
}
}
11 changes: 5 additions & 6 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,8 @@ fn format_inner<'a>(
Pad::Space => write!(result, "{:1$}", v, width),
}
}?
} else {
return Err(fmt::Error); // insufficient arguments for given format
}
// do nothing if the arguments is insufficent
}

Item::Fixed(ref spec) => {
Expand Down Expand Up @@ -690,13 +689,13 @@ fn format_inner<'a>(
}
};

match ret {
Some(ret) => ret?,
None => return Err(fmt::Error), // insufficient arguments for given format
if let Some(ret) = ret {
ret?
}
}

Item::Error => return Err(fmt::Error),
// do nothing, if the format is in wrong format
Item::Error => (),
}
Ok(())
}
Expand Down