mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-09 22:28:51 +02:00
bitcoin-tx: Avoid treating overflow as OP_0
This commit is contained in:
@@ -66,16 +66,16 @@ CScript ParseScript(const std::string& s)
|
|||||||
(w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
|
(w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
|
||||||
{
|
{
|
||||||
// Number
|
// Number
|
||||||
int64_t n = LocaleIndependentAtoi<int64_t>(w);
|
const auto num{ToIntegral<int64_t>(w)};
|
||||||
|
|
||||||
// limit the range of numbers ParseScript accepts in decimal
|
// limit the range of numbers ParseScript accepts in decimal
|
||||||
// since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
|
// since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
|
||||||
if (n > int64_t{0xffffffff} || n < -1 * int64_t{0xffffffff}) {
|
if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
|
||||||
throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
|
throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
|
||||||
"range -0xFFFFFFFF...0xFFFFFFFF");
|
"range -0xFFFFFFFF...0xFFFFFFFF");
|
||||||
}
|
}
|
||||||
|
|
||||||
result << n;
|
result << num.value();
|
||||||
} else if (w.substr(0, 2) == "0x" && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
|
} else if (w.substr(0, 2) == "0x" && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
|
||||||
// Raw hex data, inserted NOT pushed onto stack:
|
// Raw hex data, inserted NOT pushed onto stack:
|
||||||
std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
|
std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ BOOST_AUTO_TEST_CASE(parse_script)
|
|||||||
{"'17'", "023137"},
|
{"'17'", "023137"},
|
||||||
{"ELSE", "67"},
|
{"ELSE", "67"},
|
||||||
{"NOP10", "b9"},
|
{"NOP10", "b9"},
|
||||||
{"11111111111111111111", "00"},
|
|
||||||
};
|
};
|
||||||
std::string all_in;
|
std::string all_in;
|
||||||
std::string all_out;
|
std::string all_out;
|
||||||
@@ -49,6 +48,7 @@ BOOST_AUTO_TEST_CASE(parse_script)
|
|||||||
}
|
}
|
||||||
BOOST_CHECK_EQUAL(HexStr(ParseScript(all_in)), all_out);
|
BOOST_CHECK_EQUAL(HexStr(ParseScript(all_in)), all_out);
|
||||||
|
|
||||||
|
BOOST_CHECK_EXCEPTION(ParseScript("11111111111111111111"), std::runtime_error, HasReason("script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF"));
|
||||||
BOOST_CHECK_EXCEPTION(ParseScript("11111111111"), std::runtime_error, HasReason("script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF"));
|
BOOST_CHECK_EXCEPTION(ParseScript("11111111111"), std::runtime_error, HasReason("script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF"));
|
||||||
BOOST_CHECK_EXCEPTION(ParseScript("OP_CHECKSIGADD"), std::runtime_error, HasReason("script parse error: unknown opcode"));
|
BOOST_CHECK_EXCEPTION(ParseScript("OP_CHECKSIGADD"), std::runtime_error, HasReason("script parse error: unknown opcode"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -294,6 +294,12 @@
|
|||||||
"output_cmp": "txcreatescript4.json",
|
"output_cmp": "txcreatescript4.json",
|
||||||
"description": "Create a new transaction with a single output script (OP_DROP) in a P2SH, wrapped in a P2SH (output as json)"
|
"description": "Create a new transaction with a single output script (OP_DROP) in a P2SH, wrapped in a P2SH (output as json)"
|
||||||
},
|
},
|
||||||
|
{ "exec": "./bitcoin-tx",
|
||||||
|
"args": ["-create", "outscript=0:999999999999999999999999999999"],
|
||||||
|
"return_code": 1,
|
||||||
|
"error_txt": "error: script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF",
|
||||||
|
"description": "Try to parse an output script with a decimal number above the allowed range"
|
||||||
|
},
|
||||||
{ "exec": "./bitcoin-tx",
|
{ "exec": "./bitcoin-tx",
|
||||||
"args": ["-create", "outscript=0:9999999999"],
|
"args": ["-create", "outscript=0:9999999999"],
|
||||||
"return_code": 1,
|
"return_code": 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user