From 79e9f6a398d5ef8af54f3ba1a5c5488e7bac5c1e Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 17 Feb 2012 18:53:57 -0700 Subject: [PATCH] [ISSUE #2554 #2567] throw if fs args for 'start' or 'end' are strings --- lib/fs.js | 8 ++++++ .../test-fs-non-number-arguments-throw.js | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 test/simple/test-fs-non-number-arguments-throw.js diff --git a/lib/fs.js b/lib/fs.js index 3603232abf80..ea7204dc3daa 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1038,8 +1038,13 @@ var ReadStream = fs.ReadStream = function(path, options) { if (this.encoding) this.setEncoding(this.encoding); if (this.start !== undefined) { + if ('number' !== typeof this.start) { + throw TypeError("'start' must be a Number or left undefined, not a string containing a number"); + } if (this.end === undefined) { this.end = Infinity; + } else if ('number' !== typeof this.end) { + throw TypeError("'end' must be a Number or left undefined, not a string containing a number"); } if (this.start > this.end) { @@ -1226,6 +1231,9 @@ var WriteStream = fs.WriteStream = function(path, options) { } if (this.start !== undefined) { + if ('number' !== typeof this.start) { + throw TypeError("'start' must be a Number or left undefined, not a string containing a number"); + } if (this.start < 0) { throw new Error('start must be >= zero'); } diff --git a/test/simple/test-fs-non-number-arguments-throw.js b/test/simple/test-fs-non-number-arguments-throw.js new file mode 100644 index 000000000000..2889253792bc --- /dev/null +++ b/test/simple/test-fs-non-number-arguments-throw.js @@ -0,0 +1,26 @@ +(function () { + "use strict"; + + var assert = require('assert'), + fs = require('./fs'), + saneEmitter; + + saneEmitter = fs.createReadStream(__filename, { start: 17, end: 29 }); + + assert.throws(function () { + fs.createReadStream(__filename, { start: "17", end: 29 }); + }, "start as string didn't throw an error for createReadStream"); + + assert.throws(function () { + fs.createReadStream(__filename, { start: 17, end: "29" }); + }, "end as string didn't throw an error"); + + assert.throws(function () { + fs.createWriteStream(__filename, { start: "17" }); + }, "start as string didn't throw an error for createWriteStream"); + + saneEmitter.on('data', function (data) { + // a sanity check when using numbers instead of strings + assert.strictEqual('"use strict";', data.toString('utf8'), 'read ' + data.toString('utf8') + ' instead of "use strict";'); + }); +}());