Skip to content

Commit

Permalink
fs: : avoid out-of-bounds arguments index access
Browse files Browse the repository at this point in the history
This updates fs to prevent accessing out-of-range indices on the arguments object, which is known to cause V8 optimization bailout.

Related to issues discussed here: nodejs#10323
  • Loading branch information
tjhosford committed Feb 2, 2017
1 parent 773cdc3 commit 0f90020
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ fs.existsSync = function(path) {
};

fs.readFile = function(path, options, callback) {
callback = maybeCallback(arguments[arguments.length - 1]);
if (arguments.length) {
callback = maybeCallback(arguments[arguments.length - 1]);
} else {
callback = rethrow();
}
options = getOptions(options, { flag: 'r' });

if (!nullCheck(path, callback))
Expand Down Expand Up @@ -534,7 +538,13 @@ function modeNum(m, def) {
}

fs.open = function(path, flags, mode, callback_) {
var callback = makeCallback(arguments[arguments.length - 1]);
var callback;

if (arguments.length) {
callback = makeCallback(arguments[arguments.length - 1]);
} else {
callback = rethrow();
}
mode = modeNum(mode, 0o666);

if (!nullCheck(path, callback)) return;
Expand Down Expand Up @@ -857,7 +867,13 @@ function preprocessSymlinkDestination(path, type, linkPath) {

fs.symlink = function(target, path, type_, callback_) {
var type = (typeof type_ === 'string' ? type_ : null);
var callback = makeCallback(arguments[arguments.length - 1]);
var callback;

if (arguments.length) {
callback = makeCallback(arguments[arguments.length - 1]);
} else {
callback = rethrow();
}

if (!nullCheck(target, callback)) return;
if (!nullCheck(path, callback)) return;
Expand Down Expand Up @@ -1074,8 +1090,7 @@ fs.futimesSync = function(fd, atime, mtime) {
binding.futimes(fd, atime, mtime);
};

function writeAll(fd, isUserFd, buffer, offset, length, position, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {

// write(fd, buffer, offset, length, position, callback)
fs.write(fd, buffer, offset, length, position, function(writeErr, written) {
Expand Down Expand Up @@ -1107,7 +1122,11 @@ function writeAll(fd, isUserFd, buffer, offset, length, position, callback_) {
}

fs.writeFile = function(path, data, options, callback) {
callback = maybeCallback(arguments[arguments.length - 1]);
if (arguments.length) {
callback = maybeCallback(arguments[arguments.length - 1]);
} else {
callback = rethrow();
}
options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' });
const flag = options.flag || 'w';

Expand Down Expand Up @@ -1161,7 +1180,12 @@ fs.writeFileSync = function(path, data, options) {
};

fs.appendFile = function(path, data, options, callback) {
callback = maybeCallback(arguments[arguments.length - 1]);
if (arguments.length) {
callback = maybeCallback(arguments[arguments.length - 1]);
} else {
callback = rethrow();
}

options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'a' });

// Don't make changes directly on options object
Expand Down

0 comments on commit 0f90020

Please sign in to comment.