From 009821290f4f260c9aed57dd82fa9d48837b25c7 Mon Sep 17 00:00:00 2001 From: Sean Date: Tue, 9 Oct 2018 13:53:26 -0400 Subject: [PATCH] Fixes #499: Update functionality for fs.truncate (#533) * Fixes #499: Update functionality for fs.truncate This change to fs.truncate()'s functionality throws an error when the length to truncate by is a non-numeric string. * Refactor numeric type validation As node implements a helper function to validate numeric values, let's replicate the same logic to use throughout Filer. --- src/filesystem/implementation.js | 7 +++++++ src/shared.js | 20 +++++++++++++++++++- tests/spec/fs.truncate.spec.js | 15 +++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index 6f15e2c..2511e3f 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -39,6 +39,7 @@ var SuperNode = require('../super-node.js'); var Node = require('../node.js'); var Stats = require('../stats.js'); var Buffer = require('../buffer.js'); +const { validateInteger } = require('../shared.js'); /** * Update node times. Only passed times are modified (undefined times are ignored) @@ -1249,6 +1250,12 @@ function truncate_file(context, path, length, callback) { if(!fileData) { return callback(new Errors.EIO('Expected Buffer')); } + try { + validateInteger(length, 'len'); + } + catch (error) { + return callback(error); + } var data = new Buffer(length); data.fill(0); if(fileData) { diff --git a/src/shared.js b/src/shared.js index 8dd0229..f6827cc 100644 --- a/src/shared.js +++ b/src/shared.js @@ -1,3 +1,5 @@ +var Errors = require('./errors.js'); + function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); @@ -19,8 +21,24 @@ function u8toArray(u8) { return array; } +function validateInteger(value, name) { + let err; + + if (typeof value !== 'number') + err = new Errors.EINVAL(name, 'number', value); + + if (err) { + Error.captureStackTrace(err, validateInteger); + throw err; + } + + return value; +} + + module.exports = { guid: guid, u8toArray: u8toArray, - nop: nop + nop: nop, + validateInteger: validateInteger, }; diff --git a/tests/spec/fs.truncate.spec.js b/tests/spec/fs.truncate.spec.js index 5a8bebb..657150d 100644 --- a/tests/spec/fs.truncate.spec.js +++ b/tests/spec/fs.truncate.spec.js @@ -10,6 +10,21 @@ describe('fs.truncate', function() { expect(fs.truncate).to.be.a('function'); }); + it('should error when length is not an integer', function(done) { + var fs = util.fs(); + var contents = 'This is a file.'; + + fs.writeFile('/myfile', contents, function(error) { + if(error) throw error; + + fs.truncate('/myfile', 'notAnInteger', function(error) { + expect(error).to.exist; + expect(error.code).to.equal('EINVAL'); + done(); + }); + }); + }); + it('should error when length is negative', function(done) { var fs = util.fs(); var contents = 'This is a file.';