Skip to content

Commit

Permalink
explictly handle UB inputs in __divti3 and __modti3
Browse files Browse the repository at this point in the history
  • Loading branch information
spoonincode committed Aug 27, 2024
1 parent be2cc9c commit 473d562
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions libraries/chain/webassembly/compiler_builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ namespace eosio { namespace chain { namespace webassembly {

EOS_ASSERT(rhs != 0, arithmetic_exception, "divide by zero");

//force integer overflow to return dividend unchanged
if(lhs == std::numeric_limits<__int128>::min() && rhs == -1) {
*ret = lhs;
return;
}

lhs /= rhs;

*ret = lhs;
Expand Down Expand Up @@ -92,6 +98,12 @@ namespace eosio { namespace chain { namespace webassembly {

EOS_ASSERT(rhs != 0, arithmetic_exception, "divide by zero");

//force undefined behavior (due to lhs/rhs being an overflow) to return zero
if(lhs == std::numeric_limits<__int128>::min() && rhs == -1) {
*ret = 0;
return;
}

lhs %= rhs;
*ret = lhs;
}
Expand Down

0 comments on commit 473d562

Please sign in to comment.