From 2c4eb4aed537b17537ca14c17cc4a1d579fd1d9b Mon Sep 17 00:00:00 2001 From: satoshiotomakan <127754187+satoshiotomakan@users.noreply.github.com> Date: Fri, 10 May 2024 12:53:20 +0200 Subject: [PATCH] [Aeternity]: Catch all exceptions on `TWAnyAddressIsValid` for Aeternity coin (#3832) --- src/Coin.cpp | 37 ++++++++++++-------- tests/chains/Aeternity/AddressTests.cpp | 3 +- tests/chains/Aeternity/TWAnyAddressTests.cpp | 18 ++++++++++ 3 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 tests/chains/Aeternity/TWAnyAddressTests.cpp diff --git a/src/Coin.cpp b/src/Coin.cpp index 12b7f596563..6033666b9ef 100644 --- a/src/Coin.cpp +++ b/src/Coin.cpp @@ -210,7 +210,11 @@ bool TW::validateAddress(TWCoinType coin, const string& address, const PrefixVar // dispatch auto* dispatcher = coinDispatcher(coin); assert(dispatcher != nullptr); - return dispatcher->validateAddress(coin, address, prefix); + try { + return dispatcher->validateAddress(coin, address, prefix); + } catch (...) { + return false; + } } bool TW::validateAddress(TWCoinType coin, const std::string& string) { @@ -221,20 +225,25 @@ bool TW::validateAddress(TWCoinType coin, const std::string& string) { // dispatch auto* dispatcher = coinDispatcher(coin); assert(dispatcher != nullptr); - bool isValid = false; - // First check HRP. - if (hrp != nullptr && !std::string(hrp).empty()) { - isValid = dispatcher->validateAddress(coin, string, Bech32Prefix(hrp)); - } - // Then check UTXO - if ((p2pkh != 0 || p2sh != 0) && !isValid) { - return isValid || dispatcher->validateAddress(coin, string, Base58Prefix{.p2pkh = p2pkh, .p2sh = p2sh}); - } - // Then check normal - if (!isValid) { - isValid = dispatcher->validateAddress(coin, string, std::monostate()); + + try { + bool isValid = false; + // First check HRP. + if (hrp != nullptr && !std::string(hrp).empty()) { + isValid = dispatcher->validateAddress(coin, string, Bech32Prefix(hrp)); + } + // Then check UTXO + if ((p2pkh != 0 || p2sh != 0) && !isValid) { + return isValid || dispatcher->validateAddress(coin, string, Base58Prefix{.p2pkh = p2pkh, .p2sh = p2sh}); + } + // Then check normal + if (!isValid) { + isValid = dispatcher->validateAddress(coin, string, std::monostate()); + } + return isValid; + } catch (...) { + return false; } - return isValid; } namespace TW::internal { diff --git a/tests/chains/Aeternity/AddressTests.cpp b/tests/chains/Aeternity/AddressTests.cpp index 2334129c5d9..581a9d75b86 100644 --- a/tests/chains/Aeternity/AddressTests.cpp +++ b/tests/chains/Aeternity/AddressTests.cpp @@ -2,9 +2,9 @@ // // Copyright © 2017 Trust Wallet. +#include #include #include -#include #include namespace TW::Aeternity::tests { @@ -20,6 +20,7 @@ TEST(AeternityAddress, FromString) { auto address = Address("ak_2p5878zbFhxnrm7meL7TmqwtvBaqcBddyp5eGzZbovZ5FeVfcw"); ASSERT_EQ(address.string(), "ak_2p5878zbFhxnrm7meL7TmqwtvBaqcBddyp5eGzZbovZ5FeVfcw"); ASSERT_ANY_THROW(Address("invalid")); + ASSERT_ANY_THROW(Address("behave@wallet")); } } // namespace TW::Aeternity::tests diff --git a/tests/chains/Aeternity/TWAnyAddressTests.cpp b/tests/chains/Aeternity/TWAnyAddressTests.cpp new file mode 100644 index 00000000000..f747af3e862 --- /dev/null +++ b/tests/chains/Aeternity/TWAnyAddressTests.cpp @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright © 2017 Trust Wallet. + +#include +#include +#include + +namespace TW::Aeternity::tests { + +// `TWAnyAddressIsValid` must catch exceptions and return false. +TEST(AeternityAddress, IsValid) { + ASSERT_FALSE(TWAnyAddressIsValid(STRING("invalid").get(), TWCoinTypeAeternity)); + ASSERT_FALSE(TWAnyAddressIsValid(STRING("behave@wallet").get(), TWCoinTypeAeternity)); + ASSERT_FALSE(TWAnyAddressIsValid(STRING("a").get(), TWCoinTypeAeternity)); +} + +} // namespace TW::Aeternity::tests