Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test failures with newer msvc compilers (related to string aliasing) #4149

Closed
wants to merge 4 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions src/ripple/rpc/impl/RPCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,19 +696,31 @@ parseRippleLibSeed(Json::Value const& value)
std::optional<Seed>
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<std::optional<Seed>(const std::string&)>;
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
using seed_match_t = std::pair<const char*, string_to_seed_t>;
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved

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<Seed>(s); }},
{jss::seed_hex.c_str(), [](const std::string& s) {
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
uint128 i;
if (i.parseHex(s))
return std::optional<Seed>(Slice(i.data(), i.size()));
return std::optional<Seed>{};
}}};

// Identify which seed type is in use.
char const* seedType = nullptr;
const seed_match_t* seedType = nullptr;
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
int count = 0;
for (auto t : seedTypes)
for (auto const& t : seedTypes)
{
if (params.isMember(t))
if (params.isMember(t.first))
{
++count;
seedType = t;
seedType = &t;
}
}

Expand All @@ -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];
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
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> seed;

if (seedType == jss::seed.c_str())
seed = parseBase58<Seed>(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> seed = seedType->second(fieldContents);

if (!seed)
error = rpcError(rpcBAD_SEED);
Expand All @@ -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(),
Expand Down Expand Up @@ -811,7 +811,8 @@ keypairForSignature(Json::Value const& params, Json::Value& error)
return {};
}

if (secretType == jss::secret.c_str())
// using strcmp as pointers may not match (see https://bit.ly/3OGvf0E)
thejohnfreeman marked this conversation as resolved.
Show resolved Hide resolved
if (strcmp(secretType, jss::secret.c_str()) == 0)
{
error = RPC::make_param_error(
"The secret field is not allowed if " +
Expand All @@ -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())
// 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]);

Expand Down