summaryrefslogtreecommitdiff
path: root/src/node_buffer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/node_buffer.cc')
-rw-r--r--src/node_buffer.cc16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/node_buffer.cc b/src/node_buffer.cc
index 05d3566ae..64f077a68 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -621,8 +621,20 @@ void Compare(const FunctionCallbackInfo<Value> &args) {
size_t cmp_length = MIN(obj_a_len, obj_b_len);
int32_t val = memcmp(obj_a_data, obj_b_data, cmp_length);
- if (!val)
- val = obj_a_len - obj_b_len;
+
+ // Normalize val to be an integer in the range of [1, -1] since
+ // implementations of memcmp() can vary by platform.
+ if (val == 0) {
+ if (obj_a_len > obj_b_len)
+ val = 1;
+ else if (obj_a_len < obj_b_len)
+ val = -1;
+ } else {
+ if (val > 0)
+ val = 1;
+ else
+ val = -1;
+ }
args.GetReturnValue().Set(val);
}