Skip to content

Commit 9990be2

Browse files
committed
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent b2ab41e commit 9990be2

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/stream_base.cc

+6-1
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,14 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
189189

190190
int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
191191
CHECK(args[0]->IsObject());
192-
CHECK(Buffer::HasInstance(args[1]));
192+
193193
Environment* env = Environment::GetCurrent(args);
194194

195+
if (!args[1]->IsUint8Array()) {
196+
env->ThrowTypeError("Second argument must be a buffer");
197+
return 0;
198+
}
199+
195200
Local<Object> req_wrap_obj = args[0].As<Object>();
196201
const char* data = Buffer::Data(args[1]);
197202
size_t length = Buffer::Length(args[1]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
6+
const server = net.createServer().listen(0, common.mustCall(() => {
7+
const client = net.connect(server.address().port, common.mustCall(() => {
8+
assert.throws(() => {
9+
client.write('broken', 'buffer');
10+
}, /^TypeError: Second argument must be a buffer$/);
11+
client.destroy();
12+
server.close();
13+
}));
14+
}));

0 commit comments

Comments
 (0)