Skip to content

Commit

Permalink
Merge pull request #1526 from AntelopeIO/negate_after
Browse files Browse the repository at this point in the history
negate after converting to unsigned to avoid UB on minimum signed value
  • Loading branch information
spoonincode authored Aug 22, 2023
2 parents fb9a0e7 + ff528b6 commit 1ac0605
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion libraries/chain/webassembly/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ namespace eosio { namespace chain { namespace webassembly {
bool is_negative = (*val < 0);
unsigned __int128 val_magnitude;

// negate after conversion to unsigned to avoid possible integer overflow
if( is_negative )
val_magnitude = static_cast<unsigned __int128>(-*val); // Works even if val is at the lowest possible value of a int128_t
val_magnitude = -static_cast<unsigned __int128>(*val); // Works even if val is at the lowest possible value of a int128_t
else
val_magnitude = static_cast<unsigned __int128>(*val);

Expand Down
4 changes: 2 additions & 2 deletions libraries/wasm-jit/Source/WAST/ParseNumbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ bool tryParseInt(ParseState& state,UnsignedInt& outUnsignedInt,I64 minSignedValu
{
case t_decimalInt:
isNegative = parseSign(nextChar);
u64 = parseDecimalUnsignedInt(nextChar,state,isNegative ? U64(-minSignedValue) : maxUnsignedValue,"int literal");
u64 = parseDecimalUnsignedInt(nextChar,state,isNegative ? -U64(minSignedValue) : maxUnsignedValue,"int literal");
break;
case t_hexInt:
isNegative = parseSign(nextChar);
Expand All @@ -239,7 +239,7 @@ bool tryParseInt(ParseState& state,UnsignedInt& outUnsignedInt,I64 minSignedValu
return false;
};

outUnsignedInt = isNegative ? UnsignedInt(-I64(u64)) : UnsignedInt(u64);
outUnsignedInt = isNegative ? -UnsignedInt(u64) : UnsignedInt(u64);

++state.nextToken;
WAVM_ASSERT_THROW(nextChar <= state.string + state.nextToken->begin);
Expand Down

0 comments on commit 1ac0605

Please sign in to comment.