diff --git a/src/Makefile.test.include b/src/Makefile.test.include --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -43,6 +43,7 @@ test/coins_tests.cpp \ test/compress_tests.cpp \ test/config_tests.cpp \ + test/core_io_tests.cpp \ test/crypto_tests.cpp \ test/cuckoocache_tests.cpp \ test/dbwrapper_tests.cpp \ diff --git a/src/core_read.cpp b/src/core_read.cpp --- a/src/core_read.cpp +++ b/src/core_read.cpp @@ -62,12 +62,18 @@ int64_t n = atoi64(*w); result << n; } else if (boost::algorithm::starts_with(*w, "0x") && - (w->begin() + 2 != w->end()) && - IsHex(std::string(w->begin() + 2, w->end()))) { - // Raw hex data, inserted NOT pushed onto stack: - std::vector raw = - ParseHex(std::string(w->begin() + 2, w->end())); - result.insert(result.end(), raw.begin(), raw.end()); + (w->begin() + 2 != w->end())) { + if (IsHex(std::string(w->begin() + 2, w->end()))) { + // Raw hex data, inserted NOT pushed onto stack: + std::vector raw = + ParseHex(std::string(w->begin() + 2, w->end())); + result.insert(result.end(), raw.begin(), raw.end()); + } else { + // Should only arrive here for improperly formatted hex values + throw std::runtime_error("Hex numbers expected to be formatted " + "in full-byte chunks (ex: 0x00 " + "instead of 0x0)"); + } } else if (w->size() >= 2 && boost::algorithm::starts_with(*w, "'") && boost::algorithm::ends_with(*w, "'")) { // Single-quoted string, pushed as data. NOTE: this is poor-man's diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -69,6 +69,7 @@ coins_tests.cpp compress_tests.cpp config_tests.cpp + core_io_tests.cpp crypto_tests.cpp cuckoocache_tests.cpp dbwrapper_tests.cpp diff --git a/src/test/core_io_tests.cpp b/src/test/core_io_tests.cpp new file mode 100644 --- /dev/null +++ b/src/test/core_io_tests.cpp @@ -0,0 +1,27 @@ +// Copyright (c) 2018 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "core_io.h" +#include "test/test_bitcoin.h" + +#include +#include + +BOOST_FIXTURE_TEST_SUITE(core_io_tests, BasicTestingSetup) + +BOOST_AUTO_TEST_CASE(parse_hex_test) { + std::string s = "0x"; + BOOST_CHECK_THROW(ParseScript(s), std::runtime_error); + + for (int numZeroes = 1; numZeroes <= 32; numZeroes++) { + s += "0"; + if (numZeroes % 2 == 0) { + BOOST_CHECK_NO_THROW(ParseScript(s)); + } else { + BOOST_CHECK_THROW(ParseScript(s), std::runtime_error); + } + } +} + +BOOST_AUTO_TEST_SUITE_END()