From c6e54236e4c79a4f9ade3e078586bd84fa9e8bc6 Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Thu, 21 Apr 2022 11:45:48 -0400 Subject: [PATCH 1/4] Fix test failures with newer mscv compilers (related to string aliasing) --- src/ripple/rpc/impl/RPCHelpers.cpp | 54 ++++++++++++++++-------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/ripple/rpc/impl/RPCHelpers.cpp b/src/ripple/rpc/impl/RPCHelpers.cpp index 499f12323f3..ab0b9b9af01 100644 --- a/src/ripple/rpc/impl/RPCHelpers.cpp +++ b/src/ripple/rpc/impl/RPCHelpers.cpp @@ -696,19 +696,31 @@ parseRippleLibSeed(Json::Value const& value) std::optional getSeedFromRPC(Json::Value const& params, Json::Value& error) { - // The array should be constexpr, but that makes Visual Studio unhappy. - static char const* const seedTypes[]{ - jss::passphrase.c_str(), jss::seed.c_str(), jss::seed_hex.c_str()}; + using string_to_seed_t = + std::function(const std::string&)>; + using seed_match_t = std::pair; + + static seed_match_t const seedTypes[]{ + {jss::passphrase.c_str(), + [](const std::string& s) { return parseGenericSeed(s); }}, + {jss::seed.c_str(), + [](const std::string& s) { return parseBase58(s); }}, + {jss::seed_hex.c_str(), [](const std::string& s) { + uint128 i; + if (i.parseHex(s)) + return std::optional(Slice(i.data(), i.size())); + return std::optional{}; + }}}; // Identify which seed type is in use. - char const* seedType = nullptr; + const seed_match_t* seedType = nullptr; int count = 0; - for (auto t : seedTypes) + for (const auto& t : seedTypes) { - if (params.isMember(t)) + if (params.isMember(t.first)) { ++count; - seedType = t; + seedType = &t; } } @@ -722,28 +734,17 @@ getSeedFromRPC(Json::Value const& params, Json::Value& error) } // Make sure a string is present - if (!params[seedType].isString()) + const auto& param = params[seedType->first]; + if (!param.isString()) { - error = RPC::expected_field_error(seedType, "string"); + error = RPC::expected_field_error(seedType->first, "string"); return std::nullopt; } - auto const fieldContents = params[seedType].asString(); + auto const fieldContents = param.asString(); // Convert string to seed. - std::optional seed; - - if (seedType == jss::seed.c_str()) - seed = parseBase58(fieldContents); - else if (seedType == jss::passphrase.c_str()) - seed = parseGenericSeed(fieldContents); - else if (seedType == jss::seed_hex.c_str()) - { - uint128 s; - - if (s.parseHex(fieldContents)) - seed.emplace(Slice(s.data(), s.size())); - } + std::optional seed = seedType->second(fieldContents); if (!seed) error = rpcError(rpcBAD_SEED); @@ -757,7 +758,6 @@ keypairForSignature(Json::Value const& params, Json::Value& error) bool const has_key_type = params.isMember(jss::key_type); // All of the secret types we allow, but only one at a time. - // The array should be constexpr, but that makes Visual Studio unhappy. static char const* const secretTypes[]{ jss::passphrase.c_str(), jss::secret.c_str(), @@ -811,7 +811,8 @@ keypairForSignature(Json::Value const& params, Json::Value& error) return {}; } - if (secretType == jss::secret.c_str()) + if (strcmp(secretType, jss::secret.c_str()) == + 0) // don't assume that pointers are the same { error = RPC::make_param_error( "The secret field is not allowed if " + @@ -823,7 +824,8 @@ keypairForSignature(Json::Value const& params, Json::Value& error) // ripple-lib encodes seed used to generate an Ed25519 wallet in a // non-standard way. While we never encode seeds that way, we try // to detect such keys to avoid user confusion. - if (secretType != jss::seed_hex.c_str()) + if (strcmp(secretType, jss::seed_hex.c_str()) != + 0) // don't assume that pointers are the same { seed = RPC::parseRippleLibSeed(params[secretType]); From 901be11c707f966f5337774f55041811ba1075ac Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Wed, 27 Apr 2022 11:40:46 -0400 Subject: [PATCH 2/4] [fold] add link to bug report as suggested by John --- src/ripple/rpc/impl/RPCHelpers.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ripple/rpc/impl/RPCHelpers.cpp b/src/ripple/rpc/impl/RPCHelpers.cpp index ab0b9b9af01..db973e2357d 100644 --- a/src/ripple/rpc/impl/RPCHelpers.cpp +++ b/src/ripple/rpc/impl/RPCHelpers.cpp @@ -811,8 +811,8 @@ keypairForSignature(Json::Value const& params, Json::Value& error) return {}; } - if (strcmp(secretType, jss::secret.c_str()) == - 0) // don't assume that pointers are the same + // using strcmp as pointers may not match (see https://bit.ly/3OGvf0E) + if (strcmp(secretType, jss::secret.c_str()) == 0) { error = RPC::make_param_error( "The secret field is not allowed if " + @@ -824,8 +824,8 @@ keypairForSignature(Json::Value const& params, Json::Value& error) // ripple-lib encodes seed used to generate an Ed25519 wallet in a // non-standard way. While we never encode seeds that way, we try // to detect such keys to avoid user confusion. - if (strcmp(secretType, jss::seed_hex.c_str()) != - 0) // don't assume that pointers are the same + // using strcmp as pointers may not match (see https://bit.ly/3OGvf0E) + if (strcmp(secretType, jss::seed_hex.c_str()) != 0) { seed = RPC::parseRippleLibSeed(params[secretType]); From 5b5488d9bf8bbd9282848823b0ab14d9a7b9bad6 Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Wed, 27 Apr 2022 12:07:54 -0400 Subject: [PATCH 3/4] [fold] adopt `east const` style preference --- src/ripple/rpc/impl/RPCHelpers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ripple/rpc/impl/RPCHelpers.cpp b/src/ripple/rpc/impl/RPCHelpers.cpp index db973e2357d..b9b4ffa2ba3 100644 --- a/src/ripple/rpc/impl/RPCHelpers.cpp +++ b/src/ripple/rpc/impl/RPCHelpers.cpp @@ -715,7 +715,7 @@ getSeedFromRPC(Json::Value const& params, Json::Value& error) // Identify which seed type is in use. const seed_match_t* seedType = nullptr; int count = 0; - for (const auto& t : seedTypes) + for (auto const& t : seedTypes) { if (params.isMember(t.first)) { From 2cba78807a79501dba2615af58393b497348a25e Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Wed, 27 Apr 2022 15:23:59 -0400 Subject: [PATCH 4/4] [fold] adopt `east const` style preference --- src/ripple/rpc/impl/RPCHelpers.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/ripple/rpc/impl/RPCHelpers.cpp b/src/ripple/rpc/impl/RPCHelpers.cpp index b9b4ffa2ba3..64e6703ef98 100644 --- a/src/ripple/rpc/impl/RPCHelpers.cpp +++ b/src/ripple/rpc/impl/RPCHelpers.cpp @@ -697,15 +697,15 @@ std::optional getSeedFromRPC(Json::Value const& params, Json::Value& error) { using string_to_seed_t = - std::function(const std::string&)>; - using seed_match_t = std::pair; + std::function(std::string const&)>; + using seed_match_t = std::pair; static seed_match_t const seedTypes[]{ {jss::passphrase.c_str(), - [](const std::string& s) { return parseGenericSeed(s); }}, + [](std::string const& s) { return parseGenericSeed(s); }}, {jss::seed.c_str(), - [](const std::string& s) { return parseBase58(s); }}, - {jss::seed_hex.c_str(), [](const std::string& s) { + [](std::string const& s) { return parseBase58(s); }}, + {jss::seed_hex.c_str(), [](std::string const& s) { uint128 i; if (i.parseHex(s)) return std::optional(Slice(i.data(), i.size())); @@ -713,7 +713,7 @@ getSeedFromRPC(Json::Value const& params, Json::Value& error) }}}; // Identify which seed type is in use. - const seed_match_t* seedType = nullptr; + seed_match_t const* seedType = nullptr; int count = 0; for (auto const& t : seedTypes) { @@ -734,7 +734,7 @@ getSeedFromRPC(Json::Value const& params, Json::Value& error) } // Make sure a string is present - const auto& param = params[seedType->first]; + auto const& param = params[seedType->first]; if (!param.isString()) { error = RPC::expected_field_error(seedType->first, "string"); @@ -811,7 +811,8 @@ keypairForSignature(Json::Value const& params, Json::Value& error) return {}; } - // using strcmp as pointers may not match (see https://bit.ly/3OGvf0E) + // using strcmp as pointers may not match (see + // https://developercommunity.visualstudio.com/t/assigning-constexpr-char--to-static-cha/10021357?entry=problem) if (strcmp(secretType, jss::secret.c_str()) == 0) { error = RPC::make_param_error( @@ -824,7 +825,8 @@ keypairForSignature(Json::Value const& params, Json::Value& error) // ripple-lib encodes seed used to generate an Ed25519 wallet in a // non-standard way. While we never encode seeds that way, we try // to detect such keys to avoid user confusion. - // using strcmp as pointers may not match (see https://bit.ly/3OGvf0E) + // using strcmp as pointers may not match (see + // https://developercommunity.visualstudio.com/t/assigning-constexpr-char--to-static-cha/10021357?entry=problem) if (strcmp(secretType, jss::seed_hex.c_str()) != 0) { seed = RPC::parseRippleLibSeed(params[secretType]);