Skip to content
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

src: turn buffer type-CHECK into exception #12753

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 additions & 0 deletions test/parallel/test-stream-base-typechecking.js
Original file line number Diff line number Diff line change
@@ -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();
}));
}));