From 9115d36c88a3682f71e91504e50a5f16bc68c18d Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Thu, 31 May 2018 20:51:10 -0400 Subject: [PATCH] Sanitize the return value of memcmp() to wasm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- libraries/chain/wasm_interface.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index 007b6b873c3..245c47d46c6 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -1281,7 +1281,12 @@ class memory_api : public context_aware_api { } int memcmp( array_ptr dest, array_ptr 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 dest, int value, size_t length ) {