script: prevent UB when computing abs value for num opcode serialize

This commit is contained in:
pierrenn 2020-04-08 15:10:28 +09:00
parent 1b151e3ffc
commit 2748e87932
No known key found for this signature in database
GPG key ID: 8907445AFE45BBFA
3 changed files with 3 additions and 11 deletions

View file

@ -329,7 +329,7 @@ public:
std::vector<unsigned char> result;
const bool neg = value < 0;
uint64_t absvalue = neg ? -value : value;
uint64_t absvalue = neg ? ~static_cast<uint64_t>(value) + 1 : static_cast<uint64_t>(value);
while(absvalue)
{

View file

@ -135,11 +135,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
const CScriptNum script_num{i64};
(void)script_num.getint();
// Avoid negation failure:
// script/script.h:332:35: runtime error: negation of -9223372036854775808 cannot be represented in type 'int64_t' (aka 'long'); cast to an unsigned type to negate this value to itself
if (script_num != CScriptNum{std::numeric_limits<int64_t>::min()}) {
(void)script_num.getvch();
}
(void)script_num.getvch();
const arith_uint256 au256 = UintToArith256(u256);
assert(ArithToUint256(au256) == u256);

View file

@ -129,10 +129,6 @@ void test_one_input(const std::vector<uint8_t>& buffer)
break;
}
(void)script_num.getint();
// Avoid negation failure:
// script/script.h:332:35: runtime error: negation of -9223372036854775808 cannot be represented in type 'int64_t' (aka 'long'); cast to an unsigned type to negate this value to itself
if (script_num != CScriptNum{std::numeric_limits<int64_t>::min()}) {
(void)script_num.getvch();
}
(void)script_num.getvch();
}
}