diff options
author | isaacs <i@izs.me> | 2012-08-27 12:40:05 -0700 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-08-27 13:03:30 -0700 |
commit | e5d95ba939d29a47b57cc0edc680a5cd38db22fb (patch) | |
tree | 2180be66e8fa2ad7f085fb81617b43510cab1234 /lib/fs.js | |
parent | 05282588e00502f5cae597c0654005b0cb685c6c (diff) | |
download | nodejs-e5d95ba939d29a47b57cc0edc680a5cd38db22fb.tar.gz nodejs-e5d95ba939d29a47b57cc0edc680a5cd38db22fb.tar.bz2 nodejs-e5d95ba939d29a47b57cc0edc680a5cd38db22fb.zip |
fs: Throw early on invalid encoding args
Re #3918
Diffstat (limited to 'lib/fs.js')
-rw-r--r-- | lib/fs.js | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -52,6 +52,13 @@ var O_WRONLY = constants.O_WRONLY || 0; var isWindows = process.platform === 'win32'; +function assertEncoding(encoding) { + if (encoding && !Buffer.isEncoding(encoding)) { + throw new Error('Unknown encoding: ' + encoding); + } +} + + fs.Stats = binding.Stats; fs.Stats.prototype._checkModeProperty = function(property) { @@ -106,6 +113,8 @@ fs.readFile = function(path, encoding_) { var callback = arguments[arguments.length - 1]; if (typeof(callback) !== 'function') callback = function() {}; + assertEncoding(encoding); + // first, stat the file, so we know the size. var size; var buffer; // single buffer with file data @@ -179,6 +188,8 @@ fs.readFile = function(path, encoding_) { }; fs.readFileSync = function(path, encoding) { + assertEncoding(encoding); + var fd = fs.openSync(path, constants.O_RDONLY, 438 /*=0666*/); var size; @@ -343,6 +354,9 @@ fs.read = function(fd, buffer, offset, length, position, callback) { // legacy string interface (fd, length, position, encoding, callback) var cb = arguments[4], encoding = arguments[3]; + + assertEncoding(encoding); + position = arguments[2]; length = arguments[1]; buffer = new Buffer(length); @@ -371,6 +385,9 @@ fs.readSync = function(fd, buffer, offset, length, position) { // legacy string interface (fd, length, position, encoding, callback) legacy = true; var encoding = arguments[3]; + + assertEncoding(encoding); + position = arguments[2]; length = arguments[1]; buffer = new Buffer(length); @@ -392,6 +409,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) { // legacy string interface (fd, data, position, encoding, callback) callback = arguments[4]; position = arguments[2]; + assertEncoding(arguments[3]); buffer = new Buffer('' + arguments[1], arguments[3]); offset = 0; @@ -419,6 +437,7 @@ fs.writeSync = function(fd, buffer, offset, length, position) { if (!Buffer.isBuffer(buffer)) { // legacy string interface (fd, data, position, encoding) position = arguments[2]; + assertEncoding(arguments[3]); buffer = new Buffer('' + arguments[1], arguments[3]); offset = 0; @@ -787,6 +806,8 @@ function writeAll(fd, buffer, offset, length, position, callback) { fs.writeFile = function(path, data, encoding_, callback) { var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8'); + assertEncoding(encoding); + var callback_ = arguments[arguments.length - 1]; callback = (typeof(callback_) == 'function' ? callback_ : null); fs.open(path, 'w', 438 /*=0666*/, function(openErr, fd) { @@ -801,6 +822,8 @@ fs.writeFile = function(path, data, encoding_, callback) { }; fs.writeFileSync = function(path, data, encoding) { + assertEncoding(encoding); + var fd = fs.openSync(path, 'w'); if (!Buffer.isBuffer(data)) { data = new Buffer('' + data, encoding || 'utf8'); @@ -818,6 +841,8 @@ fs.writeFileSync = function(path, data, encoding) { fs.appendFile = function(path, data, encoding_, callback) { var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8'); + assertEncoding(encoding); + var callback_ = arguments[arguments.length - 1]; callback = (typeof(callback_) == 'function' ? callback_ : null); @@ -829,6 +854,8 @@ fs.appendFile = function(path, data, encoding_, callback) { }; fs.appendFileSync = function(path, data, encoding) { + assertEncoding(encoding); + var fd = fs.openSync(path, 'a'); if (!Buffer.isBuffer(data)) { data = new Buffer('' + data, encoding || 'utf8'); @@ -1288,6 +1315,8 @@ var ReadStream = fs.ReadStream = function(path, options) { this[key] = options[key]; } + assertEncoding(this.encoding); + if (this.encoding) this.setEncoding(this.encoding); if (this.start !== undefined) { @@ -1331,6 +1360,7 @@ util.inherits(ReadStream, Stream); fs.FileReadStream = fs.ReadStream; // support the legacy name ReadStream.prototype.setEncoding = function(encoding) { + assertEncoding(encoding); var StringDecoder = require('string_decoder').StringDecoder; // lazy load this._decoder = new StringDecoder(encoding); }; @@ -1584,6 +1614,7 @@ WriteStream.prototype.write = function(data) { if (!Buffer.isBuffer(data)) { var encoding = 'utf8'; if (typeof(arguments[1]) == 'string') encoding = arguments[1]; + assertEncoding(encoding); data = new Buffer('' + data, encoding); } @@ -1670,6 +1701,7 @@ SyncWriteStream.prototype.write = function(data, arg1, arg2) { throw new Error('bad arg'); } } + assertEncoding(encoding); // Change strings to buffers. SLOW if (typeof data == 'string') { |