fs: validate fd on fs.write
authorJulian Duque <julianduquej@gmail.com>
Tue, 28 Apr 2015 20:15:41 +0000 (15:15 -0500)
committerJulian Duque <julianduquej@gmail.com>
Tue, 28 Apr 2015 22:44:37 +0000 (17:44 -0500)
PR-URL: #1553
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
src/node_file.cc
test/parallel/test-fs-write-no-fd.js [new file with mode: 0644]

index d466acc..095710e 100644 (file)
@@ -779,7 +779,9 @@ static void Open(const FunctionCallbackInfo<Value>& args) {
 static void WriteBuffer(const FunctionCallbackInfo<Value>& 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 (file)
index 0000000..143928e
--- /dev/null
@@ -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/);