Skip to content

Commit

Permalink
Improve public_key::decode_account to not write result on error (#3794)
Browse files Browse the repository at this point in the history
public_key::decode_account() would overwrite the object it was called
on error, if the error was with the checksum part of the address.

This commit changes the function to work on a temporary object and
store the result only on success.

resolves  #3793
  • Loading branch information
dsiganos authored Apr 14, 2022
1 parent 3013b84 commit 54ad83f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 14 additions & 0 deletions nano/core_test/uint256_union.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,20 @@ TEST (uint256_union, decode_nano_variant)
ASSERT_FALSE (key.decode_account ("nano_1111111111111111111111111111111111111111111111111111hifc8npp"));
}

/**
* It used to be the case that when the address was wrong only in the checksum part
* then the decode_account would return error and it would also write the address with
* fixed checksum into 'key', which is not desirable.
*/
TEST (uint256_union, key_is_not_updated_on_checksum_error)
{
nano::account key;
ASSERT_EQ (key, 0);
bool result = key.decode_account ("nano_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtd1");
ASSERT_EQ (key, 0);
ASSERT_TRUE (result);
}

TEST (uint256_union, account_transcode)
{
nano::account value;
Expand Down
8 changes: 6 additions & 2 deletions nano/lib/numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,18 @@ bool nano::public_key::decode_account (std::string const & source_a)
}
if (!error)
{
*this = (number_l >> 40).convert_to<nano::uint256_t> ();
nano::public_key temp = (number_l >> 40).convert_to<nano::uint256_t> ();
uint64_t check (number_l & static_cast<uint64_t> (0xffffffffff));
uint64_t validation (0);
blake2b_state hash;
blake2b_init (&hash, 5);
blake2b_update (&hash, bytes.data (), bytes.size ());
blake2b_update (&hash, temp.bytes.data (), temp.bytes.size ());
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&validation), 5);
error = check != validation;
if (!error)
{
*this = temp;
}
}
}
else
Expand Down

0 comments on commit 54ad83f

Please sign in to comment.