Skip to content

Commit

Permalink
fix UB in on_second
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldreik committed Jun 3, 2019
1 parent 2740241 commit 3716491
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,19 @@ struct chrono_formatter {

if (ns == numeric_system::standard) {
write(second(), 2);
auto ms = get_milliseconds(std::chrono::duration<Rep, Period>(val));
#ifdef FMT_SAFE_DURATION_CAST
int ec;
//convert rep->Rep
using Crep=std::chrono::duration<rep, Period>;
using CRep=std::chrono::duration<Rep, Period>;
auto tmpval = safe_duration_cast::safe_duration_cast<CRep>(Crep{val}, ec);
if (ec) {
FMT_THROW(format_error("value would cause UB or the wrong result"));
}
#else
auto tmpval=std::chrono::duration<Rep, Period>(val);
#endif
auto ms = get_milliseconds(tmpval);
if (ms != std::chrono::milliseconds(0)) {
*out++ = '.';
write(ms.count(), 3);
Expand Down

0 comments on commit 3716491

Please sign in to comment.