From d81594de98e2b54de826bb04b1c4b16660cf15a1 Mon Sep 17 00:00:00 2001 From: Sean Prashad Date: Mon, 24 Sep 2018 18:22:57 -0400 Subject: [PATCH] Fixes #499: Add a test for truncate when length is NaN This change to fs.truncate()'s functionality raises an error when the supplied length variable is (1) not a valid integer, or, (2) not a positive integer. --- src/filesystem/implementation.js | 5 ++++- tests/spec/fs.truncate.spec.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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();