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

buffer: fix missing null/undefined check #2195

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
4 changes: 4 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ function fromObject(obj) {
return b;
}

if (obj == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is == faster than checking null and undefined separately?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we are going to do this, might as well check !obj; it's clearer of the intention.

Copy link
Contributor

Choose a reason for hiding this comment

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

But !obj will give different error messages with Boolean values, as false value will enter this block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cjihrig Yes. If you look at the IR produced by Hydrogen it has a single instruction for "check-null-or-undefined".

@Fishrock123 fromObject() is a catch-all for other values not parsed elsewhere. This is an explicit check for the two following:

> new Buffer(null)
TypeError: Cannot read property 'buffer' of null

> new Buffer()
TypeError: Cannot read property 'buffer' of undefined

Everything else will fall through and hit the exception at the bottom of the function.

throw new TypeError('must start with number, buffer, array or string');
}

if (obj instanceof ArrayBuffer) {
return binding.createFromArrayBuffer(obj);
}
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1179,3 +1179,7 @@ Buffer.poolSize = ps;
assert.throws(function() {
Buffer(10).copy();
});

assert.throws(function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe include an explicit test for null too, just in case the code changes in the future.

Copy link
Member

Choose a reason for hiding this comment

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

+1 to that if we're going with == null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call

new Buffer();
}, /must start with number, buffer, array or string/);