Merge bitcoin/bitcoin#27317: log: Check that the timestamp string is non-empty to avoid undefined behavior

73f4eb511c Check that the Timestamp String is valid (John Moffett)

Pull request description:

  Follow-up to https://github.com/bitcoin/bitcoin/pull/27233

  The current `FormatISO8601DateTime` function will return an empty string if it encounters an error when converting the `int64_t` seconds-since-epoch to a formatted date time. In the unlikely case that happens, here `strStamped.pop_back()` would be undefined behavior.

ACKs for top commit:
  MarcoFalke:
    lgtm ACK 73f4eb511c
  stickies-v:
    ACK 73f4eb511c

Tree-SHA512: 089ed639c193deb98870a8385039b31b4baed821ea907937bfc6f65a5b0981bbf8284b2afec81b2d0a922e2340716b48cf55349640eb6b8c311ef7af25abc361
This commit is contained in:
fanquake 2023-04-05 11:37:41 +01:00
commit 27ad26de2f
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

View File

@ -352,7 +352,7 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str)
const auto now{SystemClock::now()};
const auto now_seconds{std::chrono::time_point_cast<std::chrono::seconds>(now)};
strStamped = FormatISO8601DateTime(TicksSinceEpoch<std::chrono::seconds>(now_seconds));
if (m_log_time_micros) {
if (m_log_time_micros && !strStamped.empty()) {
strStamped.pop_back();
strStamped += strprintf(".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
}