-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: don't throw in read if buffer too big
If the resulting buffer.toString() call in fs.read throws, catch the error and pass it back in the callback. This issue only presents itself when fs.read is called using the legacy string interface: fs.read(fd, length, position, encoding, callback) PR-URL: #3503 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
- Loading branch information
Showing
2 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Buffer = require('buffer').Buffer; | ||
const kStringMaxLength = process.binding('buffer').kStringMaxLength; | ||
const kMaxLength = process.binding('buffer').kMaxLength; | ||
|
||
var fd; | ||
|
||
common.refreshTmpDir(); | ||
|
||
const file = path.join(common.tmpDir, 'toobig2.txt'); | ||
const stream = fs.createWriteStream(file, { | ||
flags: 'a' | ||
}); | ||
|
||
const size = kStringMaxLength / 200; | ||
const a = new Buffer(size).fill('a'); | ||
|
||
for (var i = 0; i < 201; i++) { | ||
stream.write(a); | ||
} | ||
|
||
stream.end(); | ||
stream.on('finish', common.mustCall(function() { | ||
fd = fs.openSync(file, 'r'); | ||
fs.read(fd, kStringMaxLength + 1, 0, 'utf8', common.mustCall(function(err) { | ||
assert.ok(err instanceof Error); | ||
assert.strictEqual('toString failed', err.message); | ||
})); | ||
})); | ||
|
||
function destroy() { | ||
try { | ||
// Make sure we close fd and unlink the file | ||
fs.closeSync(fd); | ||
fs.unlinkSync(file); | ||
} catch (err) { | ||
// it may not exist | ||
} | ||
} | ||
|
||
process.on('exit', destroy); | ||
|
||
process.on('SIGINT', function() { | ||
destroy(); | ||
process.exit(); | ||
}); | ||
|
||
// To make sure we don't leave a very large file | ||
// on test machines in the event this test fails. | ||
process.on('uncaughtException', function(err) { | ||
destroy(); | ||
throw err; | ||
}); |