Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: only memcmp if length > 0 in Buffer::Compare #2544

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@ void Compare(const FunctionCallbackInfo<Value> &args) {

size_t cmp_length = MIN(obj_a_length, obj_b_length);

int32_t val = memcmp(obj_a_data, obj_b_data, cmp_length);
int val = cmp_length > 0 ? memcmp(obj_a_data, obj_b_data, cmp_length)
: 0;

// Normalize val to be an integer in the range of [1, -1] since
// implementations of memcmp() can vary by platform.
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,9 @@ assert.equal(Buffer.compare(d, b), 1);
assert.equal(Buffer.compare(b, d), -1);
assert.equal(Buffer.compare(c, c), 0);

assert.equal(Buffer.compare(Buffer(0), Buffer(0)), 0);
assert.equal(Buffer.compare(Buffer(0), Buffer(1)), -1);
assert.equal(Buffer.compare(Buffer(1), Buffer(0)), 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-0 wouldn't change anything here, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-0 only flips the signed bit on the double. Which is dropped when the value is converted to an int.


assert.throws(function() {
var b = new Buffer(1);
Expand Down