Skip to content

Commit

Permalink
Sanitize the return value of memcmp() to wasm
Browse files Browse the repository at this point in the history
For wasm’s memcmp, we farm it out to the system's memcmp, however the c specification states that memcmp only needs to return less than 0, 0, or greater than 0. It's implementation specific how much less than or greater than 0 it is. So, sanitize the return value to only ever be -1, 0, 1.
  • Loading branch information
spoonincode committed Jun 1, 2018
1 parent 9766d2f commit 9115d36
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion libraries/chain/wasm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,12 @@ class memory_api : public context_aware_api {
}

int memcmp( array_ptr<const char> dest, array_ptr<const char> src, size_t length) {
return ::memcmp(dest, src, length);
int ret = ::memcmp(dest, src, length);
if(ret < 0)
return -1;
if(ret > 0)
return 1;
return 0;
}

char* memset( array_ptr<char> dest, int value, size_t length ) {
Expand Down

0 comments on commit 9115d36

Please sign in to comment.