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

Commit 94a5f53

Browse files
thefourtheyekevinoid
authored andcommitted
fs: don't alter user provided options object
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>
1 parent 0455848 commit 94a5f53

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

fs-file-sync-fd.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ function getOptions(options, defaultOptions) {
3838
return options;
3939
}
4040

41+
function copyObject(source, target) {
42+
target = arguments.length >= 2 ? target : {};
43+
for (const key in source)
44+
target[key] = source[key];
45+
return target;
46+
}
47+
4148
function assertEncoding(encoding) {
4249
if (encoding && !Buffer.isEncoding(encoding)) {
4350
throw new Error('Unknown encoding: ' + encoding);
@@ -165,11 +172,11 @@ fsFileSyncFD.writeFileSync = function(path, data, options) {
165172
fsFileSyncFD.appendFileSync = function(path, data, options) {
166173
options = getOptions(options, { encoding: 'utf8', mode: 438, flag: 'a' });
167174

168-
if (!options.flag)
169-
options = util._extend({ flag: 'a' }, options);
175+
// Don't make changes directly on options object
176+
options = copyObject(options);
170177

171178
// force append behavior when using a supplied file descriptor
172-
if (isFd(path))
179+
if (!options.flag || isFd(path))
173180
options.flag = 'a';
174181

175182
fsFileSyncFD.writeFileSync(path, data, options);

0 commit comments

Comments
 (0)