Skip to content
Permalink
Browse files

src: turn buffer type-CHECK into exception

Turn a `CHECK()` that could be brought to fail using public APIs
into throwing an error.

Fixes: #12152
PR-URL: #12753
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information...
addaleax committed Apr 29, 2017
1 parent b2ab41e commit 9990be2919fb73b2285118a236dc6f7adbdf7cef
Showing with 20 additions and 1 deletion.
  1. +6 −1 src/stream_base.cc
  2. +14 −0 test/parallel/test-stream-base-typechecking.js
@@ -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]);
@@ -0,0 +1,14 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

const server = net.createServer().listen(0, common.mustCall(() => {
const client = net.connect(server.address().port, common.mustCall(() => {
assert.throws(() => {
client.write('broken', 'buffer');
}, /^TypeError: Second argument must be a buffer$/);
client.destroy();
server.close();
}));
}));

0 comments on commit 9990be2

Please sign in to comment.
You can’t perform that action at this time.