mirror of https://github.com/lukechilds/node.git
Browse Source
This patch 1. moves the basic validation of arguments to `truncate` family of functions to the JavaScript layer from the C++ layer. 2. makes sure that the File Descriptors are validated strictly. PR-URL: https://github.com/nodejs/node/pull/2498 Reviewed-By: Trevor Norris <trev.norris@gmail.com>v7.x
Sakthipriyan Vairamani
9 years ago
6 changed files with 301 additions and 118 deletions
@ -1,21 +1,71 @@ |
|||
'use strict'; |
|||
var common = require('../common'); |
|||
var assert = require('assert'); |
|||
var path = require('path'); |
|||
var fs = require('fs'); |
|||
var tmp = common.tmpDir; |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const fs = require('fs'); |
|||
const path = require('path'); |
|||
|
|||
common.refreshTmpDir(); |
|||
var filename = path.resolve(tmp, 'truncate-file.txt'); |
|||
const fds = []; |
|||
|
|||
const filename = path.resolve(common.tmpDir, 'truncate-file.txt'); |
|||
fs.writeFileSync(filename, 'hello world', 'utf8'); |
|||
var fd = fs.openSync(filename, 'r+'); |
|||
const fd = fs.openSync(filename, 'r+'); |
|||
fds.push(fd); |
|||
fs.truncate(fd, 5, common.mustCall(function(err) { |
|||
assert.ok(!err); |
|||
assert.ifError(err); |
|||
assert.equal(fs.readFileSync(filename, 'utf8'), 'hello'); |
|||
})); |
|||
|
|||
process.on('exit', function() { |
|||
fs.closeSync(fd); |
|||
fs.unlinkSync(filename); |
|||
console.log('ok'); |
|||
}); |
|||
{ |
|||
// test partial truncation of a file
|
|||
const fileName = path.resolve(common.tmpDir, 'truncate-file-1.txt'); |
|||
console.log(fileName); |
|||
fs.writeFileSync(fileName, 'hello world', 'utf8'); |
|||
const fd = fs.openSync(fileName, 'r+'); |
|||
fds.push(fd); |
|||
|
|||
fs.truncate(fd, 5, common.mustCall(function(err) { |
|||
assert.ifError(err); |
|||
assert.strictEqual(fs.readFileSync(fileName, 'utf8'), 'hello'); |
|||
})); |
|||
} |
|||
|
|||
{ |
|||
// make sure numbers as strings are not treated as fds with sync version
|
|||
const fileName = path.resolve(common.tmpDir, 'truncate-file-2.txt'); |
|||
console.log(fileName); |
|||
fs.writeFileSync(fileName, 'One'); |
|||
const fd = fs.openSync(fileName, 'r'); |
|||
fds.push(fd); |
|||
|
|||
const fdFileName = path.resolve(common.tmpDir, '' + fd); |
|||
fs.writeFileSync(fdFileName, 'Two'); |
|||
assert.strictEqual(fs.readFileSync(fileName).toString(), 'One'); |
|||
assert.strictEqual(fs.readFileSync(fdFileName).toString(), 'Two'); |
|||
|
|||
fs.truncateSync(fdFileName); |
|||
assert.strictEqual(fs.readFileSync(fileName).toString(), 'One'); |
|||
assert.strictEqual(fs.readFileSync(fdFileName).toString(), ''); |
|||
} |
|||
|
|||
{ |
|||
// make sure numbers as strings are not treated as fds with async version
|
|||
const fileName = path.resolve(common.tmpDir, 'truncate-file-3.txt'); |
|||
console.log(fileName); |
|||
fs.writeFileSync(fileName, 'One'); |
|||
const fd = fs.openSync(fileName, 'r'); |
|||
fds.push(fd); |
|||
|
|||
const fdFileName = path.resolve(common.tmpDir, '' + fd); |
|||
fs.writeFileSync(fdFileName, 'Two'); |
|||
assert.strictEqual(fs.readFileSync(fileName).toString(), 'One'); |
|||
assert.strictEqual(fs.readFileSync(fdFileName).toString(), 'Two'); |
|||
|
|||
fs.truncate(fdFileName, common.mustCall(function(err) { |
|||
assert.ifError(err); |
|||
assert.strictEqual(fs.readFileSync(fileName).toString(), 'One'); |
|||
assert.strictEqual(fs.readFileSync(fdFileName).toString(), ''); |
|||
})); |
|||
} |
|||
|
|||
process.on('exit', () => fds.forEach((fd) => fs.closeSync(fd))); |
|||
|
@ -1,12 +1,13 @@ |
|||
'use strict'; |
|||
|
|||
require('../common'); |
|||
const fs = require('fs'); |
|||
const assert = require('assert'); |
|||
|
|||
assert.throws(function() { |
|||
fs.write(null, Buffer.allocUnsafe(1), 0, 1); |
|||
}, /TypeError/); |
|||
fs.write(null, Buffer.allocUnsafe(1), 0, 1, () => {}); |
|||
}, /TypeError: file descriptor must be a unsigned 32-bit integer/); |
|||
|
|||
assert.throws(function() { |
|||
fs.write(null, '1', 0, 1); |
|||
}, /TypeError/); |
|||
fs.write(undefined, '1', 0, 1, () => {}); |
|||
}, /TypeError: file descriptor must be a unsigned 32-bit integer/); |
|||
|
Loading…
Reference in new issue