-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
buffer: throw exception when creating from non-Node.js Context #23938
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -271,7 +271,10 @@ MaybeLocal<Object> New(Isolate* isolate, size_t length) { | |
EscapableHandleScope handle_scope(isolate); | ||
Local<Object> obj; | ||
Environment* env = Environment::GetCurrent(isolate); | ||
CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here. | ||
if (env == nullptr) { | ||
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate); | ||
return MaybeLocal<Object>(); | ||
} | ||
if (Buffer::New(env, length).ToLocal(&obj)) | ||
return handle_scope.Escape(obj); | ||
return Local<Object>(); | ||
|
@@ -314,7 +317,10 @@ MaybeLocal<Object> New(Environment* env, size_t length) { | |
MaybeLocal<Object> Copy(Isolate* isolate, const char* data, size_t length) { | ||
EscapableHandleScope handle_scope(isolate); | ||
Environment* env = Environment::GetCurrent(isolate); | ||
CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here. | ||
if (env == nullptr) { | ||
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate); | ||
return MaybeLocal<Object>(); | ||
} | ||
Local<Object> obj; | ||
if (Buffer::Copy(env, data, length).ToLocal(&obj)) | ||
return handle_scope.Escape(obj); | ||
|
@@ -364,7 +370,11 @@ MaybeLocal<Object> New(Isolate* isolate, | |
void* hint) { | ||
EscapableHandleScope handle_scope(isolate); | ||
Environment* env = Environment::GetCurrent(isolate); | ||
CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here. | ||
if (env == nullptr) { | ||
callback(data, hint); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Belated review: this Performing the action only sometimes is confusing, IMO. A caller that diligently checks the return value and frees the memory when it's empty will now (sometimes) double-free. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bnoordhuis I think it was more or less the idea to be more consistent here and always take ownership, because in other cases we already did free the data when failing (e.g. in the |
||
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate); | ||
return MaybeLocal<Object>(); | ||
} | ||
Local<Object> obj; | ||
if (Buffer::New(env, data, length, callback, hint).ToLocal(&obj)) | ||
return handle_scope.Escape(obj); | ||
|
@@ -403,7 +413,11 @@ MaybeLocal<Object> New(Environment* env, | |
MaybeLocal<Object> New(Isolate* isolate, char* data, size_t length) { | ||
EscapableHandleScope handle_scope(isolate); | ||
Environment* env = Environment::GetCurrent(isolate); | ||
CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here. | ||
if (env == nullptr) { | ||
free(data); | ||
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate); | ||
return MaybeLocal<Object>(); | ||
} | ||
Local<Object> obj; | ||
if (Buffer::New(env, data, length).ToLocal(&obj)) | ||
return handle_scope.Escape(obj); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const { | ||
makeBufferInNewContext | ||
} = require(`./build/${common.buildType}/binding`); | ||
|
||
// Because the `Buffer` function and its protoype property only (currently) | ||
// exist in a Node.js instance’s main context, trying to create buffers from | ||
// another context throws an exception. | ||
|
||
try { | ||
makeBufferInNewContext(); | ||
} catch (exception) { | ||
assert.strictEqual(exception.constructor.name, 'Error'); | ||
assert(!(exception.constructor instanceof Error)); | ||
|
||
assert.strictEqual(exception.code, 'ERR_BUFFER_CONTEXT_NOT_AVAILABLE'); | ||
assert.strictEqual(exception.message, | ||
'Buffer is not available for the current Context'); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.