Skip to content
Open
1 change: 0 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,6 @@ AM_CONDITIONAL([ENABLE_QT_TESTS], [test "$BUILD_TEST_QT" = "yes"])
AM_CONDITIONAL([ENABLE_BENCH], [test "$use_bench" = "yes"])
AM_CONDITIONAL([USE_QRCODE], [test "$use_qr" = "yes"])
AM_CONDITIONAL([USE_LCOV], [test "$use_lcov" = "yes"])
AM_CONDITIONAL([USE_LIBEVENT], [test "$use_libevent" = "yes"])
AM_CONDITIONAL([HARDEN], [test "$use_hardening" = "yes"])
AM_CONDITIONAL([ENABLE_SSSE3], [test "$enable_ssse3" = "yes"])
AM_CONDITIONAL([ENABLE_SSE42], [test "$enable_sse42" = "yes"])
Expand Down
8 changes: 2 additions & 6 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ BITCOIN_CORE_H = \
util/sock.h \
util/string.h \
util/spanparsing.h \
util/subprocess.hpp \
util/subprocess.h \
util/syserror.h \
util/system.h \
util/time.h \
Expand Down Expand Up @@ -921,6 +921,7 @@ libbitcoin_common_a_SOURCES = \
coins.cpp \
common/bloom.cpp \
common/run_command.cpp \
common/url.cpp \
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider to do bitcoin#29967 to addition for bitcoin#29904

compressor.cpp \
core_read.cpp \
core_write.cpp \
Expand Down Expand Up @@ -958,11 +959,6 @@ libbitcoin_common_a_SOURCES = \
script/standard.cpp \
warnings.cpp \
$(BITCOIN_CORE_H)

if USE_LIBEVENT
libbitcoin_common_a_CPPFLAGS += $(EVENT_CFLAGS)
libbitcoin_common_a_SOURCES += common/url.cpp
endif
#

# util #
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ BITCOIN_TESTS =\
test/cachemultimap_tests.cpp \
test/coins_tests.cpp \
test/coinstatsindex_tests.cpp \
test/common_url_tests.cpp \
test/compilerbug_tests.cpp \
test/compress_tests.cpp \
test/crypto_tests.cpp \
Expand Down
2 changes: 0 additions & 2 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <chainparamsbase.h>
#include <clientversion.h>
#include <common/url.h>
#include <compat/compat.h>
#include <compat/stdin.h>
#include <policy/feerate.h>
Expand Down Expand Up @@ -49,7 +48,6 @@
using CliClock = std::chrono::system_clock;

const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
UrlDecodeFn* const URL_DECODE = urlDecode;

static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900;
Expand Down
2 changes: 0 additions & 2 deletions src/bitcoin-wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <chainparams.h>
#include <chainparamsbase.h>
#include <common/url.h>
#include <compat/compat.h>
#include <interfaces/init.h>
#include <logging.h>
Expand All @@ -25,7 +24,6 @@
#include <string>
#include <tuple>
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
UrlDecodeFn* const URL_DECODE = nullptr;

static void SetupWalletToolArgs(ArgsManager& argsman)
{
Expand Down
2 changes: 0 additions & 2 deletions src/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <chainparams.h>
#include <clientversion.h>
#include <common/url.h>
#include <compat/compat.h>
#include <init.h>
#include <interfaces/chain.h>
Expand All @@ -34,7 +33,6 @@
using node::NodeContext;

const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
UrlDecodeFn* const URL_DECODE = urlDecode;

#if HAVE_DECL_FORK

Expand Down
2 changes: 1 addition & 1 deletion src/common/run_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <univalue.h>

#ifdef ENABLE_EXTERNAL_SIGNER
#include <util/subprocess.hpp>
#include <util/subprocess.h>
#endif // ENABLE_EXTERNAL_SIGNER

UniValue RunCommandParseJSON(const std::string& str_command, const std::string& str_std_in)
Expand Down
35 changes: 26 additions & 9 deletions src/common/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,36 @@

#include <common/url.h>

#include <event2/http.h>

#include <cstdlib>
#include <charconv>
#include <string>
#include <string_view>
#include <system_error>

std::string urlDecode(const std::string &urlEncoded) {
std::string UrlDecode(std::string_view url_encoded)
{
std::string res;
if (!urlEncoded.empty()) {
char *decoded = evhttp_uridecode(urlEncoded.c_str(), false, nullptr);
if (decoded) {
res = std::string(decoded);
free(decoded);
res.reserve(url_encoded.size());

for (size_t i = 0; i < url_encoded.size(); ++i) {
char c = url_encoded[i];
// Special handling for percent which should be followed by two hex digits
// representing an octet values, see RFC 3986, Section 2.1 Percent-Encoding
if (c == '%' && i + 2 < url_encoded.size()) {
unsigned int decoded_value{0};
auto [p, ec] = std::from_chars(url_encoded.data() + i + 1, url_encoded.data() + i + 3, decoded_value, 16);

// Only if there is no error and the pointer is set to the end of
// the string, we can be sure both characters were valid hex
if (ec == std::errc{} && p == url_encoded.data() + i + 3) {
res += static_cast<char>(decoded_value);
// Next two characters are part of the percent encoding
i += 2;
continue;
}
// In case of invalid percent encoding, add the '%' and continue
}
res += c;
}

return res;
}
9 changes: 6 additions & 3 deletions src/common/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
#define BITCOIN_COMMON_URL_H

#include <string>
#include <string_view>

using UrlDecodeFn = std::string(const std::string& url_encoded);
UrlDecodeFn urlDecode;
extern UrlDecodeFn* const URL_DECODE;
/* Decode a URL.
*
* Notably this implementation does not decode a '+' to a ' '.
*/
std::string UrlDecode(std::string_view url_encoded);

#endif // BITCOIN_COMMON_URL_H
2 changes: 0 additions & 2 deletions src/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <qt/bitcoin.h>

#include <common/url.h>
#include <compat/compat.h>
#include <util/translation.h>

Expand All @@ -17,7 +16,6 @@
extern const std::function<std::string(const char*)> G_TRANSLATION_FUN = [](const char* psz) {
return QCoreApplication::translate("dash-core", psz).toStdString();
};
UrlDecodeFn* const URL_DECODE = urlDecode;

MAIN_FUNCTION
{
Expand Down
72 changes: 72 additions & 0 deletions src/test/common_url_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2024-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://opensource.org/license/mit/.

#include <common/url.h>

#include <string>

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(common_url_tests)

// These test vectors were ported from test/regress.c in the libevent library
// which used to be a dependency of the UrlDecode function.

BOOST_AUTO_TEST_CASE(encode_decode_test) {
BOOST_CHECK_EQUAL(UrlDecode("Hello"), "Hello");
BOOST_CHECK_EQUAL(UrlDecode("99"), "99");
BOOST_CHECK_EQUAL(UrlDecode(""), "");
BOOST_CHECK_EQUAL(UrlDecode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_"),
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_");
BOOST_CHECK_EQUAL(UrlDecode("%20"), " ");
BOOST_CHECK_EQUAL(UrlDecode("%FF%F0%E0"), "\xff\xf0\xe0");
BOOST_CHECK_EQUAL(UrlDecode("%01%19"), "\x01\x19");
BOOST_CHECK_EQUAL(UrlDecode("http%3A%2F%2Fwww.ietf.org%2Frfc%2Frfc3986.txt"),
"http://www.ietf.org/rfc/rfc3986.txt");
BOOST_CHECK_EQUAL(UrlDecode("1%2B2%3D3"), "1+2=3");
}

BOOST_AUTO_TEST_CASE(decode_malformed_test) {
BOOST_CHECK_EQUAL(UrlDecode("%%xhello th+ere \xff"), "%%xhello th+ere \xff");

BOOST_CHECK_EQUAL(UrlDecode("%"), "%");
BOOST_CHECK_EQUAL(UrlDecode("%%"), "%%");
BOOST_CHECK_EQUAL(UrlDecode("%%%"), "%%%");
BOOST_CHECK_EQUAL(UrlDecode("%%%%"), "%%%%");

BOOST_CHECK_EQUAL(UrlDecode("+"), "+");
BOOST_CHECK_EQUAL(UrlDecode("++"), "++");

BOOST_CHECK_EQUAL(UrlDecode("?"), "?");
BOOST_CHECK_EQUAL(UrlDecode("??"), "??");

BOOST_CHECK_EQUAL(UrlDecode("%G1"), "%G1");
BOOST_CHECK_EQUAL(UrlDecode("%2"), "%2");
BOOST_CHECK_EQUAL(UrlDecode("%ZX"), "%ZX");

BOOST_CHECK_EQUAL(UrlDecode("valid%20string%G1"), "valid string%G1");
BOOST_CHECK_EQUAL(UrlDecode("%20invalid%ZX"), " invalid%ZX");
BOOST_CHECK_EQUAL(UrlDecode("%20%G1%ZX"), " %G1%ZX");

BOOST_CHECK_EQUAL(UrlDecode("%1 "), "%1 ");
BOOST_CHECK_EQUAL(UrlDecode("% 9"), "% 9");
BOOST_CHECK_EQUAL(UrlDecode(" %Z "), " %Z ");
BOOST_CHECK_EQUAL(UrlDecode(" % X"), " % X");

BOOST_CHECK_EQUAL(UrlDecode("%-1"), "%-1");
BOOST_CHECK_EQUAL(UrlDecode("%1-"), "%1-");
}

BOOST_AUTO_TEST_CASE(decode_lowercase_hex_test) {
BOOST_CHECK_EQUAL(UrlDecode("%f0%a0%b0"), "\xf0\xa0\xb0");
}

BOOST_AUTO_TEST_CASE(decode_internal_nulls_test) {
std::string result1{"\0\0x\0\0", 5};
BOOST_CHECK_EQUAL(UrlDecode("%00%00x%00%00"), result1);
std::string result2{"abc\0\0", 5};
BOOST_CHECK_EQUAL(UrlDecode("abc%00%00"), result2);
}

BOOST_AUTO_TEST_SUITE_END()
4 changes: 2 additions & 2 deletions src/test/fuzz/poolresource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class PoolResourceFuzzer
{
if (m_total_allocated > 0x1000000) return;
size_t alignment_bits = m_provider.ConsumeIntegralInRange<size_t>(0, 7);
size_t alignment = 1 << alignment_bits;
size_t alignment = size_t{1} << alignment_bits;
size_t size_bits = m_provider.ConsumeIntegralInRange<size_t>(0, 16 - alignment_bits);
size_t size = m_provider.ConsumeIntegralInRange<size_t>(1U << size_bits, (1U << (size_bits + 1)) - 1U) << alignment_bits;
size_t size = m_provider.ConsumeIntegralInRange<size_t>(size_t{1} << size_bits, (size_t{1} << (size_bits + 1)) - 1U) << alignment_bits;
Allocate(size, alignment);
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ FUZZ_TARGET(string)
(void)ToUpper(random_string_1);
(void)TrimString(random_string_1);
(void)TrimString(random_string_1, random_string_2);
(void)urlDecode(random_string_1);
(void)UrlDecode(random_string_1);
(void)ContainsNoNUL(random_string_1);
(void)_(random_string_1.c_str());
try {
Expand Down
2 changes: 1 addition & 1 deletion src/test/system_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <univalue.h>

#ifdef ENABLE_EXTERNAL_SIGNER
#include <util/subprocess.hpp>
#include <util/subprocess.h>
#endif // ENABLE_EXTERNAL_SIGNER

#include <boost/test/unit_test.hpp>
Expand Down
2 changes: 0 additions & 2 deletions src/test/util/setup_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <chainlock/chainlock.h>
#include <chainlock/handler.h>
#include <chainparams.h>
#include <common/url.h>
#include <consensus/consensus.h>
#include <consensus/merkle.h>
#include <consensus/params.h>
Expand Down Expand Up @@ -95,7 +94,6 @@ using node::fPruneMode;
using node::fReindex;

const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
UrlDecodeFn* const URL_DECODE = nullptr;

FastRandomContext g_insecure_rand_ctx;
/** Random context to get unique temp data dirs. Separate from g_insecure_rand_ctx, which can be seeded from a const env var */
Expand Down
7 changes: 4 additions & 3 deletions src/test/validation_chainstate_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <test/util/chainstate.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <util/check.h>
#include <validation.h>

#include <vector>
Expand Down Expand Up @@ -108,14 +109,14 @@ BOOST_FIXTURE_TEST_CASE(chainstate_update_tip, TestChain100Setup)

BOOST_CHECK_EQUAL(chainman.GetAll().size(), 2);

CChainState& background_cs{*[&] {
CChainState& background_cs{*Assert([&]() -> CChainState* {
for (CChainState* cs : chainman.GetAll()) {
if (cs != &chainman.ActiveChainstate()) {
return cs;
}
}
assert(false);
}()};
return nullptr;
}())};

// Create a block to append to the validation chain.
std::vector<CMutableTransaction> noTxns;
Expand Down
17 changes: 9 additions & 8 deletions src/txorphanage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
#include <logging.h>
#include <policy/policy.h>
#include <stats/client.h>
#include <util/time.h>

#include <cassert>

/** Expiration time for orphan transactions in seconds */
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
/** Minimum time between orphan transactions expire time checks in seconds */
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
/** Expiration time for orphan transactions */
static constexpr auto ORPHAN_TX_EXPIRE_TIME{20min};
/** Minimum time between orphan transactions expire time checks */
static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};


bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
Expand All @@ -39,7 +40,7 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
return false;
}

auto ret = m_orphans.emplace(hash, OrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME, m_orphan_list.size(), sz});
auto ret = m_orphans.emplace(hash, OrphanTx{tx, peer, Now<NodeSeconds>() + ORPHAN_TX_EXPIRE_TIME, m_orphan_list.size(), sz});
assert(ret.second);
m_orphan_list.push_back(ret.first);
for (const CTxIn& txin : tx->vin) {
Expand Down Expand Up @@ -121,12 +122,12 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans_size)
LOCK(m_mutex);

unsigned int nEvicted = 0;
static int64_t nNextSweep;
int64_t nNow = GetTime();
static NodeSeconds nNextSweep;
auto nNow{Now<NodeSeconds>()};
if (nNextSweep <= nNow) {
// Sweep out expired orphan pool entries:
int nErased = 0;
int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
auto nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
std::map<uint256, OrphanTx>::iterator iter = m_orphans.begin();
while (iter != m_orphans.end())
{
Expand Down
3 changes: 2 additions & 1 deletion src/txorphanage.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <sync.h>
#include <util/time.h>

#include <map>
#include <set>
Expand Down Expand Up @@ -70,7 +71,7 @@ class TxOrphanage {
struct OrphanTx {
CTransactionRef tx;
NodeId fromPeer;
int64_t nTimeExpire;
NodeSeconds nTimeExpire;
size_t list_pos;
size_t nTxSize;
};
Expand Down
Loading
Loading