Skip to content

Commit

Permalink
Tighten up bech32::Decode(); add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
murrayn committed Apr 13, 2018
1 parent ad960f5 commit 60f61f9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/bech32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ std::pair<std::string, data> 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');
Expand All @@ -172,7 +172,8 @@ std::pair<std::string, data> 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 {};
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/bech32_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 60f61f9

Please sign in to comment.