diff options
author | Trevor Norris <trev.norris@gmail.com> | 2014-04-29 14:46:58 -0700 |
---|---|---|
committer | Trevor Norris <trev.norris@gmail.com> | 2014-04-30 15:42:39 -0700 |
commit | 51f49ddb6615432b1a6437a8c932f9d2ce2a71cf (patch) | |
tree | 7e28a2be6d213e49be3939ddbe6fa640d891db4c /src | |
parent | d0fc5538d1ac769885c7e449b0d1f6759a7d4230 (diff) | |
download | node-buf-compare-normalize.tar.gz |
buffer: normalize compare() outputbuf-compare-normalize
Because of differences in memcmp() implementation, normalize output to
return -1, 0 or 1 only.
Diffstat (limited to 'src')
-rw-r--r-- | src/node_buffer.cc | 16 |
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); } |