From 6ef674f22cfac6556db0d6e4b5d0268eba71d14f Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 30 Apr 2017 01:48:37 +0200 Subject: [PATCH] src: turn buffer type-CHECK into exception Turn a `CHECK()` that could be brought to fail using public APIs into throwing an error. Fixes: https://github.com/nodejs/node/issues/12152 --- src/stream_base.cc | 7 ++++++- test/parallel/test-stream-base-typechecking.js | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-stream-base-typechecking.js diff --git a/src/stream_base.cc b/src/stream_base.cc index 3ed622d7ef35a2..19130b5bb8bd7e 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -189,9 +189,14 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { int StreamBase::WriteBuffer(const FunctionCallbackInfo& 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 req_wrap_obj = args[0].As(); const char* data = Buffer::Data(args[1]); size_t length = Buffer::Length(args[1]); diff --git a/test/parallel/test-stream-base-typechecking.js b/test/parallel/test-stream-base-typechecking.js new file mode 100644 index 00000000000000..d1b8bfc699c845 --- /dev/null +++ b/test/parallel/test-stream-base-typechecking.js @@ -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$/);