Skip to content

Commit fa43e7c

Browse files
author
MarcoFalke
committed
bitcoin-tx: Avoid treating overflow as OP_0
1 parent fa053c0 commit fa43e7c

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/core_read.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ CScript ParseScript(const std::string& s)
6666
(w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
6767
{
6868
// Number
69-
int64_t n = LocaleIndependentAtoi<int64_t>(w);
69+
const auto num{ToIntegral<int64_t>(w)};
7070

7171
// limit the range of numbers ParseScript accepts in decimal
7272
// since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
73-
if (n > int64_t{0xffffffff} || n < -1 * int64_t{0xffffffff}) {
73+
if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
7474
throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
7575
"range -0xFFFFFFFF...0xFFFFFFFF");
7676
}
7777

78-
result << n;
78+
result << num.value();
7979
} else if (w.substr(0, 2) == "0x" && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
8080
// Raw hex data, inserted NOT pushed onto stack:
8181
std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));

src/test/script_parse_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ BOOST_AUTO_TEST_CASE(parse_script)
3838
{"'17'", "023137"},
3939
{"ELSE", "67"},
4040
{"NOP10", "b9"},
41-
{"11111111111111111111", "00"},
4241
};
4342
std::string all_in;
4443
std::string all_out;
@@ -49,6 +48,7 @@ BOOST_AUTO_TEST_CASE(parse_script)
4948
}
5049
BOOST_CHECK_EQUAL(HexStr(ParseScript(all_in)), all_out);
5150

51+
BOOST_CHECK_EXCEPTION(ParseScript("11111111111111111111"), std::runtime_error, HasReason("script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF"));
5252
BOOST_CHECK_EXCEPTION(ParseScript("11111111111"), std::runtime_error, HasReason("script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF"));
5353
BOOST_CHECK_EXCEPTION(ParseScript("OP_CHECKSIGADD"), std::runtime_error, HasReason("script parse error: unknown opcode"));
5454
}

test/util/data/bitcoin-util-test.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@
294294
"output_cmp": "txcreatescript4.json",
295295
"description": "Create a new transaction with a single output script (OP_DROP) in a P2SH, wrapped in a P2SH (output as json)"
296296
},
297+
{ "exec": "./bitcoin-tx",
298+
"args": ["-create", "outscript=0:999999999999999999999999999999"],
299+
"return_code": 1,
300+
"error_txt": "error: script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF",
301+
"description": "Try to parse an output script with a decimal number above the allowed range"
302+
},
297303
{ "exec": "./bitcoin-tx",
298304
"args": ["-create", "outscript=0:9999999999"],
299305
"return_code": 1,

0 commit comments

Comments
 (0)