operator-(const time_point &, const duration &) mishandles unsigned numbers
- Dominant language
- C++
- Stars
- 20
- Forks
- 58
- PR merge metrics
- No merged PRs in 30d
Description
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
#include
#include
#include
// 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 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.
Contributor guide
No contributing guide indexed for this repository
Research direction
Locate the implementation of operator-(const time_point&, const duration&) in the Boost.Chrono sources and inspect how subtraction is expressed for unsigned representations. Add a regression test based on the uint32_t duration example, verifying that subtracting one microsecond-scaled unit produces the expected negative time point.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100