diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index 6f15e2c..753be93 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -1277,7 +1277,10 @@ function truncate_file(context, path, length, callback) { } } - if(length < 0) { + if (!Number.isInteger(length)) { + callback(new Errors.EINVAL('length must be a number')); + } + else if (length < 0) { callback(new Errors.EINVAL('length cannot be negative')); } else { find_node(context, path, read_file_data); diff --git a/tests/spec/fs.truncate.spec.js b/tests/spec/fs.truncate.spec.js index 4827ffe..6577752 100644 --- a/tests/spec/fs.truncate.spec.js +++ b/tests/spec/fs.truncate.spec.js @@ -25,6 +25,21 @@ describe('fs.truncate', function() { }); }); + it('should error when length is NaN', 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', 'NaN', function(error) { + expect(error).to.exist; + expect(error.code).to.equal('EINVAL'); + done(); + }); + }); + }); + it('should error when path is not a file', function(done) { var fs = util.fs();