Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

negate after converting to unsigned to avoid UB on minimum signed value #1526

Merged
merged 4 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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