Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ce04b73

Browse filesBrowse files
skomskirvagg
authored andcommitted
src: only memcmp if length > 0 in Buffer::Compare
Both pointer arguments to memcmp are defined as non-null and compiler optimizes upon that. PR-URL: #2544 Reviewed-By: trevnorris - Trevor Norris <trev.norris@gmail.com> Reviewed-By: thefourtheye - Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent 42e075a commit ce04b73
Copy full SHA for ce04b73

File tree

Expand file treeCollapse file tree

2 files changed

+4
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+4
-1
lines changed
Open diff view settings
Collapse file

‎src/node_buffer.cc‎

Copy file name to clipboardExpand all lines: src/node_buffer.cc
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ void Compare(const FunctionCallbackInfo<Value> &args) {
835835

836836
size_t cmp_length = MIN(obj_a_length, obj_b_length);
837837

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

840840
// Normalize val to be an integer in the range of [1, -1] since
841841
// implementations of memcmp() can vary by platform.
Collapse file

‎test/parallel/test-buffer.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-buffer.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,9 @@ assert.equal(Buffer.compare(d, b), 1);
11371137
assert.equal(Buffer.compare(b, d), -1);
11381138
assert.equal(Buffer.compare(c, c), 0);
11391139

1140+
assert.equal(Buffer.compare(Buffer(0), Buffer(0)), 0);
1141+
assert.equal(Buffer.compare(Buffer(0), Buffer(1)), -1);
1142+
assert.equal(Buffer.compare(Buffer(1), Buffer(0)), 1);
11401143

11411144
assert.throws(function() {
11421145
var b = new Buffer(1);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.