Clarify the safety of NetClock::time_point arithmetic:

* NetClock::rep is uint32_t and can be error-prone when
  used with subtraction.
* Fixes #3656
This commit is contained in:
Howard Hinnant
2021-03-09 16:48:49 -05:00
committed by manojsdoshi
parent 9d89d4c188
commit 64e4a89470
3 changed files with 4 additions and 3 deletions

View File

@@ -638,7 +638,7 @@ CreateOffer::takerCross(
Sandbox& sbCancel, Sandbox& sbCancel,
Amounts const& takerAmount) Amounts const& takerAmount)
{ {
NetClock::time_point const when{ctx_.view().parentCloseTime()}; NetClock::time_point const when = ctx_.view().parentCloseTime();
beast::WrappedSink takerSink(j_, "Taker "); beast::WrappedSink takerSink(j_, "Taker ");

View File

@@ -152,7 +152,9 @@ isCurrent(
// Because this can be called on untrusted, possibly // Because this can be called on untrusted, possibly
// malicious validations, we do our math in a way // malicious validations, we do our math in a way
// that avoids any chance of overflowing or underflowing // that avoids any chance of overflowing or underflowing
// the signing time. // the signing time. All of the expressions below are
// promoted from unsigned 32 bit to signed 64 bit prior
// to computation.
return (signTime > (now - p.validationCURRENT_EARLY)) && return (signTime > (now - p.validationCURRENT_EARLY)) &&
(signTime < (now + p.validationCURRENT_WALL)) && (signTime < (now + p.validationCURRENT_WALL)) &&

View File

@@ -268,7 +268,6 @@ verifyHandshake(
// We can't blindly "return a-b;" because TimeKeeper::time_point // We can't blindly "return a-b;" because TimeKeeper::time_point
// uses an unsigned integer for representing durations, which is // uses an unsigned integer for representing durations, which is
// a problem when trying to subtract time points. // a problem when trying to subtract time points.
// FIXME: @HowardHinnant, should we migrate to using std::int64_t?
auto calculateOffset = [](TimeKeeper::time_point a, auto calculateOffset = [](TimeKeeper::time_point a,
TimeKeeper::time_point b) { TimeKeeper::time_point b) {
if (a > b) if (a > b)