Skip to content

Commit

Permalink
src: turn buffer type-CHECK into exception
Browse files Browse the repository at this point in the history
Turn a `CHECK()` that could be brought to fail using public APIs
into throwing an error.

Fixes: nodejs#12152
  • Loading branch information
addaleax committed May 3, 2017
1 parent 8aca66a commit 6ef674f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,14 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {

int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsObject());
CHECK(Buffer::HasInstance(args[1]));

Environment* env = Environment::GetCurrent(args);

if (!args[1]->IsUint8Array()) {
env->ThrowTypeError("Second argument must be a buffer");
return 0;
}

Local<Object> req_wrap_obj = args[0].As<Object>();
const char* data = Buffer::Data(args[1]);
size_t length = Buffer::Length(args[1]);
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-stream-base-typechecking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';
require('../common');
const assert = require('assert');

assert.throws(() => {
process.stdout.write('broken', 'buffer');
}, /^TypeError: Second argument must be a buffer$/);

0 comments on commit 6ef674f

Please sign in to comment.