Skip to content

Commit ae69fc3

Browse files
committed
Merge bitcoin/bitcoin#31391: util: Drop boost posix_time in ParseISO8601DateTime
faf70cc Remove wallet::ParseISO8601DateTime, use ParseISO8601DateTime instead (MarcoFalke) 2222aec util: Implement ParseISO8601DateTime based on C++20 (MarcoFalke) Pull request description: `boost::posix_time` in `ParseISO8601DateTime` has many issues: * It parses random strings that are clearly invalid and returns a time value for them, see [1] below. * None of the separators `-`, or `:`, or `T`, or `Z` are validated. * It may crash when running under a hardened C++ library, see bitcoin/bitcoin#28917. * It has been unmaintained for years, so reporting or fixing any issues will most likely be useless. * It pulls in a third-party dependency, when the functionality is already included in vanilla C++20. Fix all issues by replacing it with a simple helper function written in C++20. Fixes bitcoin/bitcoin#28917. [1] The following patch passes on current master: ```diff diff --git a/src/wallet/test/rpc_util_tests.cpp b/src/wallet/test/rpc_util_tests.cpp index 32f6f5a..c1c94c7116 100644 --- a/src/wallet/test/rpc_util_tests.cpp +++ b/src/wallet/test/rpc_util_tests.cpp @@ -12,6 +12,14 @@ BOOST_AUTO_TEST_SUITE(wallet_util_tests) BOOST_AUTO_TEST_CASE(util_ParseISO8601DateTime) { + BOOST_CHECK_EQUAL(ParseISO8601DateTime("964296"), 242118028800); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("244622"), 15023836800); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("+INfINITy"), 9223372036854); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("7000802 01"), 158734166400); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("7469-2 +INfINITy"), 9223372036854); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("maXimum-datE-time"), 253402300799); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("577737 114maXimum-datE-time"), 253402300799); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0); BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0); BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:01Z"), 946684801); ``` ACKs for top commit: hebasto: ACK faf70cc, I have reviewed the code and it looks OK. dergoegge: utACK faf70cc Tree-SHA512: 9dd745a356d04acf6200e13a6af52c51a9e2a0eeccea110093ce5da147b3c669c0eda918e46db0164c081a78c8feae3fe557a4759bea18449a8ff2d090095931
2 parents ff873a2 + faf70cc commit ae69fc3

File tree

14 files changed

+94
-69
lines changed

14 files changed

+94
-69
lines changed

src/test/fuzz/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ add_executable(fuzz
7676
p2p_transport_serialization.cpp
7777
package_eval.cpp
7878
parse_hd_keypath.cpp
79+
parse_iso8601.cpp
7980
parse_numbers.cpp
8081
parse_script.cpp
8182
parse_univalue.cpp
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// Copyright (c) 2019-2022 The Bitcoin Core developers
1+
// Copyright (c) 2019-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <test/fuzz/FuzzedDataProvider.h>
66
#include <test/fuzz/fuzz.h>
77
#include <util/time.h>
8-
#include <wallet/rpc/util.h>
98

109
#include <cassert>
1110
#include <cstdint>
@@ -21,14 +20,8 @@ FUZZ_TARGET(parse_iso8601)
2120

2221
const std::string iso8601_datetime = FormatISO8601DateTime(random_time);
2322
(void)FormatISO8601Date(random_time);
24-
const int64_t parsed_time_1 = wallet::ParseISO8601DateTime(iso8601_datetime);
25-
if (random_time >= 0) {
26-
assert(parsed_time_1 >= 0);
27-
if (iso8601_datetime.length() == 20) {
28-
assert(parsed_time_1 == random_time);
29-
}
30-
}
23+
const int64_t parsed_time_1{ParseISO8601DateTime(iso8601_datetime).value()};
24+
assert(parsed_time_1 == random_time);
3125

32-
const int64_t parsed_time_2 = wallet::ParseISO8601DateTime(random_string);
33-
assert(parsed_time_2 >= 0);
26+
(void)ParseISO8601DateTime(random_string);
3427
}

src/test/fuzz/util.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021-2022 The Bitcoin Core developers
1+
// Copyright (c) 2021-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -34,8 +34,8 @@ CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::option
3434
int64_t ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optional<int64_t>& min, const std::optional<int64_t>& max) noexcept
3535
{
3636
// Avoid t=0 (1970-01-01T00:00:00Z) since SetMockTime(0) disables mocktime.
37-
static const int64_t time_min{946684801}; // 2000-01-01T00:00:01Z
38-
static const int64_t time_max{4133980799}; // 2100-12-31T23:59:59Z
37+
static const int64_t time_min{ParseISO8601DateTime("2000-01-01T00:00:01Z").value()};
38+
static const int64_t time_max{ParseISO8601DateTime("2100-12-31T23:59:59Z").value()};
3939
return fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(min.value_or(time_min), max.value_or(time_max));
4040
}
4141

src/test/util_tests.cpp

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2022 The Bitcoin Core developers
1+
// Copyright (c) 2011-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -323,20 +323,65 @@ BOOST_AUTO_TEST_CASE(util_TrimString)
323323
BOOST_CHECK_EQUAL(TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), "");
324324
}
325325

326+
BOOST_AUTO_TEST_CASE(util_ParseISO8601DateTime)
327+
{
328+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1969-12-31T23:59:59Z").value(), -1);
329+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z").value(), 0);
330+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:01Z").value(), 1);
331+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:01Z").value(), 946684801);
332+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z").value(), 1317425777);
333+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2100-12-31T23:59:59Z").value(), 4133980799);
334+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("9999-12-31T23:59:59Z").value(), 253402300799);
335+
336+
// Accept edge-cases, where the time overflows. They are not produced by
337+
// FormatISO8601DateTime, so this can be changed in the future, if needed.
338+
// For now, keep compatibility with the previous implementation.
339+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T99:00:00Z").value(), 947041200);
340+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:99:00Z").value(), 946690740);
341+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:99Z").value(), 946684899);
342+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T99:99:99Z").value(), 947047239);
343+
344+
// Reject date overflows.
345+
BOOST_CHECK(!ParseISO8601DateTime("2000-99-01T00:00:00Z"));
346+
BOOST_CHECK(!ParseISO8601DateTime("2000-01-99T00:00:00Z"));
347+
348+
// Reject out-of-range years
349+
BOOST_CHECK(!ParseISO8601DateTime("32768-12-31T23:59:59Z"));
350+
BOOST_CHECK(!ParseISO8601DateTime("32767-12-31T23:59:59Z"));
351+
BOOST_CHECK(!ParseISO8601DateTime("32767-12-31T00:00:00Z"));
352+
BOOST_CHECK(!ParseISO8601DateTime("999-12-31T00:00:00Z"));
353+
354+
// Reject invalid format
355+
const std::string valid{"2000-01-01T00:00:01Z"};
356+
BOOST_CHECK(ParseISO8601DateTime(valid).has_value());
357+
for (auto mut{0U}; mut < valid.size(); ++mut) {
358+
std::string invalid{valid};
359+
invalid[mut] = 'a';
360+
BOOST_CHECK(!ParseISO8601DateTime(invalid));
361+
}
362+
}
363+
326364
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
327365
{
328366
BOOST_CHECK_EQUAL(FormatISO8601DateTime(971890963199), "32767-12-31T23:59:59Z");
329367
BOOST_CHECK_EQUAL(FormatISO8601DateTime(971890876800), "32767-12-31T00:00:00Z");
330-
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
368+
369+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(-1), "1969-12-31T23:59:59Z");
331370
BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z");
371+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1), "1970-01-01T00:00:01Z");
372+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(946684801), "2000-01-01T00:00:01Z");
373+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
374+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(4133980799), "2100-12-31T23:59:59Z");
375+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(253402300799), "9999-12-31T23:59:59Z");
332376
}
333377

334378
BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)
335379
{
336380
BOOST_CHECK_EQUAL(FormatISO8601Date(971890963199), "32767-12-31");
337381
BOOST_CHECK_EQUAL(FormatISO8601Date(971890876800), "32767-12-31");
338-
BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
382+
339383
BOOST_CHECK_EQUAL(FormatISO8601Date(0), "1970-01-01");
384+
BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
340385
}
341386

342387
BOOST_AUTO_TEST_CASE(util_FormatMoney)

src/util/time.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2022 The Bitcoin Core developers
2+
// Copyright (c) 2009-present The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -8,10 +8,13 @@
88
#include <compat/compat.h>
99
#include <tinyformat.h>
1010
#include <util/check.h>
11+
#include <util/strencodings.h>
1112

1213
#include <atomic>
1314
#include <chrono>
15+
#include <optional>
1416
#include <string>
17+
#include <string_view>
1518
#include <thread>
1619

1720
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
@@ -60,6 +63,30 @@ std::string FormatISO8601Date(int64_t nTime)
6063
return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
6164
}
6265

66+
std::optional<int64_t> ParseISO8601DateTime(std::string_view str)
67+
{
68+
constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()};
69+
if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') {
70+
return {};
71+
}
72+
const auto year{ToIntegral<uint16_t>(str.substr(0, 4))};
73+
const auto month{ToIntegral<uint8_t>(str.substr(5, 2))};
74+
const auto day{ToIntegral<uint8_t>(str.substr(8, 2))};
75+
const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))};
76+
const auto min{ToIntegral<uint8_t>(str.substr(14, 2))};
77+
const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))};
78+
if (!year || !month || !day || !hour || !min || !sec) {
79+
return {};
80+
}
81+
const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}};
82+
if (!ymd.ok()) {
83+
return {};
84+
}
85+
const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}};
86+
const auto tp{std::chrono::sys_days{ymd} + time};
87+
return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)};
88+
}
89+
6390
struct timeval MillisToTimeval(int64_t nTimeout)
6491
{
6592
struct timeval timeout;

src/util/time.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2022 The Bitcoin Core developers
2+
// Copyright (c) 2009-present The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -8,7 +8,9 @@
88

99
#include <chrono> // IWYU pragma: export
1010
#include <cstdint>
11+
#include <optional>
1112
#include <string>
13+
#include <string_view>
1214

1315
using namespace std::chrono_literals;
1416

@@ -105,6 +107,7 @@ T GetTime()
105107
*/
106108
std::string FormatISO8601DateTime(int64_t nTime);
107109
std::string FormatISO8601Date(int64_t nTime);
110+
std::optional<int64_t> ParseISO8601DateTime(std::string_view str);
108111

109112
/**
110113
* Convert milliseconds to a struct timeval for e.g. select.

src/wallet/rpc/backup.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2009-2022 The Bitcoin Core developers
1+
// Copyright (c) 2009-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -549,7 +549,7 @@ RPCHelpMan importwallet()
549549
continue;
550550
CKey key = DecodeSecret(vstr[0]);
551551
if (key.IsValid()) {
552-
int64_t nTime = ParseISO8601DateTime(vstr[1]);
552+
int64_t nTime{ParseISO8601DateTime(vstr[1]).value_or(0)};
553553
std::string strLabel;
554554
bool fLabel = true;
555555
for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
@@ -569,7 +569,7 @@ RPCHelpMan importwallet()
569569
} else if(IsHex(vstr[0])) {
570570
std::vector<unsigned char> vData(ParseHex(vstr[0]));
571571
CScript script = CScript(vData.begin(), vData.end());
572-
int64_t birth_time = ParseISO8601DateTime(vstr[1]);
572+
int64_t birth_time{ParseISO8601DateTime(vstr[1]).value_or(0)};
573573
if (birth_time > 0) nTimeBegin = std::min(nTimeBegin, birth_time);
574574
scripts.emplace_back(script, birth_time);
575575
}

src/wallet/rpc/util.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2022 The Bitcoin Core developers
1+
// Copyright (c) 2011-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -14,26 +14,10 @@
1414
#include <string_view>
1515
#include <univalue.h>
1616

17-
#include <boost/date_time/posix_time/posix_time.hpp>
18-
1917
namespace wallet {
2018
static const std::string WALLET_ENDPOINT_BASE = "/wallet/";
2119
const std::string HELP_REQUIRING_PASSPHRASE{"\nRequires wallet passphrase to be set with walletpassphrase call if wallet is encrypted.\n"};
2220

23-
int64_t ParseISO8601DateTime(const std::string& str)
24-
{
25-
static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
26-
static const std::locale loc(std::locale::classic(),
27-
new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
28-
std::istringstream iss(str);
29-
iss.imbue(loc);
30-
boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
31-
iss >> ptime;
32-
if (ptime.is_not_a_date_time() || epoch > ptime)
33-
return 0;
34-
return (ptime - epoch).total_seconds();
35-
}
36-
3721
bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param) {
3822
bool can_avoid_reuse = wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
3923
bool avoid_reuse = param.isNull() ? can_avoid_reuse : param.get_bool();

src/wallet/rpc/util.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2022 The Bitcoin Core developers
1+
// Copyright (c) 2017-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -51,7 +51,6 @@ std::string LabelFromValue(const UniValue& value);
5151
void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey, UniValue& entry);
5252

5353
void HandleWalletError(const std::shared_ptr<CWallet> wallet, DatabaseStatus& status, bilingual_str& error);
54-
int64_t ParseISO8601DateTime(const std::string& str);
5554
void AppendLastProcessedBlock(UniValue& entry, const CWallet& wallet) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
5655
} // namespace wallet
5756

src/wallet/test/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ target_sources(test_bitcoin
1414
init_tests.cpp
1515
ismine_tests.cpp
1616
psbt_wallet_tests.cpp
17-
rpc_util_tests.cpp
1817
scriptpubkeyman_tests.cpp
1918
spend_tests.cpp
2019
wallet_crypto_tests.cpp

0 commit comments

Comments
 (0)