Skip to content

Commit 4969579

Browse files
fs: check type for option argument
1 parent 66db924 commit 4969579

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

lib/fs.js

+10
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,11 @@ function ReadStream(path, options) {
16151615
if (!(this instanceof ReadStream))
16161616
return new ReadStream(path, options);
16171617

1618+
if (typeof options === 'string')
1619+
options = { encoding: options };
1620+
else if (options && typeof options !== 'object')
1621+
throw new TypeError('Bad arguments');
1622+
16181623
// a little bit bigger buffer and water marks by default
16191624
options = Object.create(options || {});
16201625
if (options.highWaterMark === undefined)
@@ -1781,6 +1786,11 @@ function WriteStream(path, options) {
17811786
if (!(this instanceof WriteStream))
17821787
return new WriteStream(path, options);
17831788

1789+
if (typeof options === 'string')
1790+
options = { encoding: options };
1791+
else if (options && typeof options !== 'object')
1792+
throw new TypeError('Bad arguments');
1793+
17841794
options = options || {};
17851795

17861796
Writable.call(this, options);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const common = require('../common');
2+
const assert = require('assert');
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const example = path.join(common.fixturesDir, 'x.txt');
7+
assert.throws(function() { fs.createReadStream(example, 123); }, TypeError);
8+
assert.throws(function() { fs.createReadStream(example, true); }, TypeError);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const common = require('../common');
2+
const assert = require('assert');
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const example = path.join(common.tmpDir, '/dummy');
7+
assert.throws(function() { fs.createReadStream(example, 123); }, TypeError);
8+
assert.throws(function() { fs.createReadStream(example, true); }, TypeError);

0 commit comments

Comments
 (0)