Skip to content

Commit 1600966

Browse files
thefourtheyeevanlucas
authored andcommitted
fs: execute mkdtemp's callback with no context
All the callback functions in `fs` module are supposed to be executed with no context (`this` value should not be a valid object). But `mkdtemp`'s callback will have the `FSReqWrap` object as the context. Sample code to reproduce the problem 'use strict'; const fs = require('fs'); fs.mkdtemp('/tmp/abcd', null, function() { console.log(this); }); This would print FSReqWrap { oncomplete: [Function] } But that should have printed `null` and this patch fixes that. PR-URL: #7068 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2961f06 commit 1600966

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

lib/fs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1590,8 +1590,7 @@ fs.realpath = function realpath(path, options, callback) {
15901590
};
15911591

15921592

1593-
fs.mkdtemp = function(prefix, options, callback_) {
1594-
var callback = maybeCallback(callback_);
1593+
fs.mkdtemp = function(prefix, options, callback) {
15951594
if (!prefix || typeof prefix !== 'string')
15961595
throw new TypeError('filename prefix is required');
15971596

@@ -1605,6 +1604,7 @@ fs.mkdtemp = function(prefix, options, callback_) {
16051604
if (typeof options !== 'object')
16061605
throw new TypeError('"options" must be a string or an object');
16071606

1607+
callback = makeCallback(callback);
16081608
if (!nullCheck(prefix, callback)) {
16091609
return;
16101610
}

test/parallel/test-fs-mkdtemp.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ assert.equal(Buffer.byteLength(path.basename(utf8)),
1818
Buffer.byteLength('\u0222abc.XXXXXX'));
1919
assert(common.fileExists(utf8));
2020

21-
fs.mkdtemp(
22-
path.join(common.tmpDir, 'bar.'),
23-
common.mustCall(function(err, folder) {
24-
assert.ifError(err);
25-
assert(common.fileExists(folder));
26-
})
27-
);
21+
function handler(err, folder) {
22+
assert.ifError(err);
23+
assert(common.fileExists(folder));
24+
assert.strictEqual(this, null);
25+
}
2826

27+
fs.mkdtemp(path.join(common.tmpDir, 'bar.'), common.mustCall(handler));
28+
29+
// Same test as above, but making sure that passing an options object doesn't
30+
// affect the way the callback function is handled.
31+
fs.mkdtemp(path.join(common.tmpDir, 'bar.'), {}, common.mustCall(handler));
32+
33+
// Making sure that not passing a callback doesn't crash, as a default function
34+
// is passed internally.
2935
assert.doesNotThrow(() => fs.mkdtemp(path.join(common.tmpDir, 'bar-')));
36+
assert.doesNotThrow(() => fs.mkdtemp(path.join(common.tmpDir, 'bar-'), {}));

0 commit comments

Comments
 (0)