From 60f61f99529f54f85c847d61122c70c0358ebecc Mon Sep 17 00:00:00 2001 From: murrayn Date: Wed, 4 Apr 2018 03:24:25 -0700 Subject: [PATCH] Tighten up bech32::Decode(); add tests. --- src/bech32.cpp | 7 ++++--- src/test/bech32_tests.cpp | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bech32.cpp b/src/bech32.cpp index 274782e46..c55f22b9b 100644 --- a/src/bech32.cpp +++ b/src/bech32.cpp @@ -160,9 +160,9 @@ std::pair Decode(const std::string& str) { bool lower = false, upper = false; for (size_t i = 0; i < str.size(); ++i) { unsigned char c = str[i]; - if (c < 33 || c > 126) return {}; if (c >= 'a' && c <= 'z') lower = true; - if (c >= 'A' && c <= 'Z') upper = true; + else if (c >= 'A' && c <= 'Z') upper = true; + else if (c < 33 || c > 126) return {}; } if (lower && upper) return {}; size_t pos = str.rfind('1'); @@ -172,7 +172,8 @@ std::pair Decode(const std::string& str) { data values(str.size() - 1 - pos); for (size_t i = 0; i < str.size() - 1 - pos; ++i) { unsigned char c = str[i + pos + 1]; - int8_t rev = (c < 33 || c > 126) ? -1 : CHARSET_REV[c]; + int8_t rev = CHARSET_REV[c]; + if (rev == -1) { return {}; } diff --git a/src/test/bech32_tests.cpp b/src/test/bech32_tests.cpp index 495290c8d..22b77e309 100644 --- a/src/test/bech32_tests.cpp +++ b/src/test/bech32_tests.cpp @@ -57,6 +57,8 @@ BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid) "A1G7SGD8", "10a06t8", "1qzzfhee", + "a12UEL5L", + "A12uEL5L", }; for (const std::string& str : CASES) { auto ret = bech32::Decode(str);