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

Formatting chrono duration with unsigned count triggers -Wsign-compare #2004

Closed
pgroke-dt opened this issue Nov 10, 2020 · 2 comments
Closed

Comments

@pgroke-dt
Copy link

Formatting a chrono duration with an unsigned count type triggers -Wsign-compare.

The culprit is the to_nonnegative_int function for integers:

// Converts value to int and checks that it's in the range [0, upper).
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
inline int to_nonnegative_int(T value, int upper) {
  FMT_ASSERT(value >= 0 && value <= upper, "invalid value");
  (void)upper;
  return static_cast<int>(value);
}

Changing the assert to e.g. FMT_ASSERT(value >= 0 && value <= T{} + upper, "invalid value"); should fix this.

Also note that the implementation doesn't assert what the comment says. The comment says upper) (=exclusive) but the implementation checks for <= upper (inclusive). (The same is also true for the implementation for floating point types BTW.)
Changing this would however also require changing the line

    time.tm_hour = to_nonnegative_int(hour12(), 12);

in chrono_formatter::on_12_hour since 12 is actually a possible value there.

@vitaut
Copy link
Contributor

vitaut commented Nov 11, 2020

Could you submit a PR?

vitaut added a commit that referenced this issue Nov 12, 2020
@vitaut
Copy link
Contributor

vitaut commented Nov 12, 2020

Suppressed in 8f426c1.

@vitaut vitaut closed this as completed Nov 12, 2020
vitaut added a commit that referenced this issue Nov 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants