[BugFix] Fix int256_t negation undefined behavior causing memory allocation failure (backport #62510) (#62578)
Signed-off-by: stephen <stephen5217@163.com> Co-authored-by: stephen <91597003+stephen-shelby@users.noreply.github.com>
This commit is contained in:
parent
dacc9100f5
commit
016d806bdd
|
|
@ -729,6 +729,11 @@ std::string int256_t::to_string() const {
|
|||
return "0";
|
||||
}
|
||||
|
||||
// Handle INT256_MIN special case directly
|
||||
if (*this == INT256_MIN) {
|
||||
return "-57896044618658097711785492504343953926634992332820282019728792003956564819968";
|
||||
}
|
||||
|
||||
int256_t temp = *this;
|
||||
bool negative = false;
|
||||
if (temp.high < 0) {
|
||||
|
|
|
|||
|
|
@ -189,6 +189,11 @@ public:
|
|||
/// @return Negated value using two's complement
|
||||
constexpr int256_t operator-() const {
|
||||
if (low == 0) {
|
||||
// Handle INT256_MIN special case to avoid undefined behavior
|
||||
if (high == static_cast<int128_t>(static_cast<uint128_t>(1) << 127)) {
|
||||
// For INT256_MIN, return itself since -INT256_MIN cannot be represented
|
||||
return *this;
|
||||
}
|
||||
return int256_t(-high, 0);
|
||||
} else {
|
||||
uint128_t new_low = ~low + 1;
|
||||
|
|
|
|||
Loading…
Reference in New Issue