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.
This commit is contained in:
Sean Prashad 2018-09-24 18:22:57 -04:00
parent fd3de6be2c
commit d81594de98
No known key found for this signature in database
GPG Key ID: 7489778C4BA96636
2 changed files with 19 additions and 1 deletions

View File

@ -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')); callback(new Errors.EINVAL('length cannot be negative'));
} else { } else {
find_node(context, path, read_file_data); find_node(context, path, read_file_data);

View File

@ -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) { it('should error when path is not a file', function(done) {
var fs = util.fs(); var fs = util.fs();