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

operator-(const time_point &, const duration &) mishandles unsigned numbers #68

Open
jlanca opened this issue Jan 25, 2023 · 0 comments

Comments

@jlanca
Copy link

jlanca commented Jan 25, 2023

The binary subtraction operation tp - d is implemented as tp + (-d).
Because the unary minus operator is used, the result is wrong if the underlying representation type is unsigned.

For instance :

#include <cassert>
#include <chrono>
#include <iostream>
#include <boost/chrono.hpp>
 
// namespace chrono = std::chrono;
// using std::micro;
namespace chrono = boost::chrono;
using boost::micro;
 
int main() {
 
  const auto tp = chrono::system_clock::time_point(chrono::milliseconds(0));
  chrono::duration<uint32_t, micro> delay(1);
  std::cout << (tp - delay).time_since_epoch().count() << std::endl;
  assert((tp - delay).time_since_epoch().count() == -1000);
  return 0;
}

Running this program produces the following output:

4294967295000
a: /tmp/a.cpp:16: int main(): Assertion `(tp - delay).time_since_epoch().count() == -1000' failed.

Note that the std::chrono from gcc does not exhibit this defect.

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

1 participant