From f9c681cf627caf89b5c7eaacf154fdefb747c7e0 Mon Sep 17 00:00:00 2001 From: Julian Duque Date: Tue, 28 Apr 2015 15:15:41 -0500 Subject: [PATCH] fs: validate fd on fs.write PR-URL: #1553 Reviewed-By: Colin Ihrig --- src/node_file.cc | 4 +++- test/parallel/test-fs-write-no-fd.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-fs-write-no-fd.js diff --git a/src/node_file.cc b/src/node_file.cc index d466acc65a9dc0..095710ef37e32b 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -779,7 +779,9 @@ static void Open(const FunctionCallbackInfo& args) { static void WriteBuffer(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - CHECK(args[0]->IsInt32()); + if (!args[0]->IsInt32()) + return env->ThrowTypeError("First argument must be file descriptor"); + CHECK(Buffer::HasInstance(args[1])); int fd = args[0]->Int32Value(); diff --git a/test/parallel/test-fs-write-no-fd.js b/test/parallel/test-fs-write-no-fd.js new file mode 100644 index 00000000000000..143928e7836ea7 --- /dev/null +++ b/test/parallel/test-fs-write-no-fd.js @@ -0,0 +1,11 @@ +const common = require('../common'); +const fs = require('fs'); +const assert = require('assert'); + +assert.throws(function() { + fs.write(null, new Buffer(1), 0, 1); +}, /TypeError/); + +assert.throws(function() { + fs.write(null, '1', 0, 1); +}, /TypeError/);