Skip to content
This repository has been archived by the owner on May 27, 2019. It is now read-only.

Commit

Permalink
fs: don't alter user provided options object
Browse files Browse the repository at this point in the history
This patch makes a copy of the `options` object before the fs module
functions alter it.

PR-URL: nodejs/node#7831
Fixes: nodejs/node#7655
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Nicu Micleușanu <micnic90@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
  • Loading branch information
thefourtheye authored and kevinoid committed Dec 13, 2016
1 parent 0455848 commit 44b0030
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions fs-file-sync-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ function getOptions(options, defaultOptions) {
return options;
}

function copyObject(source, target) {
target = arguments.length >= 2 ? target : {};
for (var key in source)
target[key] = source[key];
return target;
}

function assertEncoding(encoding) {
if (encoding && !Buffer.isEncoding(encoding)) {
throw new Error('Unknown encoding: ' + encoding);
Expand Down Expand Up @@ -165,11 +172,11 @@ fsFileSyncFD.writeFileSync = function(path, data, options) {
fsFileSyncFD.appendFileSync = function(path, data, options) {
options = getOptions(options, { encoding: 'utf8', mode: 438, flag: 'a' });

if (!options.flag)
options = util._extend({ flag: 'a' }, options);
// Don't make changes directly on options object
options = copyObject(options);

// force append behavior when using a supplied file descriptor
if (isFd(path))
if (!options.flag || isFd(path))
options.flag = 'a';

fsFileSyncFD.writeFileSync(path, data, options);
Expand Down

0 comments on commit 44b0030

Please sign in to comment.