Skip to content

Commit 1317252

Browse files
VoltrexKeyvaaduh95
authored andcommitted
fs: allow empty string for temp directory prefix
The `fs` lib module's `mkdtemp()` and `mkdtempSync()` methods were missing a validator, and weren't allowing the empty string as a valid prefix. PR-URL: #39028 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Zijian Liu <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Khaidi Chu <[email protected]>
1 parent 46a7e61 commit 1317252

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

doc/api/fs.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,10 @@ rejection only when `recursive` is false.
802802
### `fsPromises.mkdtemp(prefix[, options])`
803803
<!-- YAML
804804
added: v10.0.0
805+
changes:
806+
- version: REPLACEME
807+
pr-url: https://github.com/nodejs/node/pull/39028
808+
description: The `prefix` parameter now accepts an empty string.
805809
-->
806810
807811
* `prefix` {string}
@@ -2574,6 +2578,9 @@ See the POSIX mkdir(2) documentation for more details.
25742578
<!-- YAML
25752579
added: v5.10.0
25762580
changes:
2581+
- version: REPLACEME
2582+
pr-url: https://github.com/nodejs/node/pull/39028
2583+
description: The `prefix` parameter now accepts an empty string.
25772584
- version: v10.0.0
25782585
pr-url: https://github.com/nodejs/node/pull/12562
25792586
description: The `callback` parameter is no longer optional. Not passing
@@ -4509,6 +4516,10 @@ See the POSIX mkdir(2) documentation for more details.
45094516
### `fs.mkdtempSync(prefix[, options])`
45104517
<!-- YAML
45114518
added: v5.10.0
4519+
changes:
4520+
- version: REPLACEME
4521+
pr-url: https://github.com/nodejs/node/pull/39028
4522+
description: The `prefix` parameter now accepts an empty string.
45124523
-->
45134524
45144525
* `prefix` {string}

lib/fs.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ const {
7474
codes: {
7575
ERR_FS_FILE_TOO_LARGE,
7676
ERR_INVALID_ARG_VALUE,
77-
ERR_INVALID_ARG_TYPE,
7877
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
7978
},
8079
AbortError,
@@ -136,6 +135,7 @@ const {
136135
validateEncoding,
137136
validateFunction,
138137
validateInteger,
138+
validateString,
139139
} = require('internal/validators');
140140

141141
const watchers = require('internal/fs/watchers');
@@ -2712,9 +2712,8 @@ realpath.native = (path, options, callback) => {
27122712
function mkdtemp(prefix, options, callback) {
27132713
callback = makeCallback(typeof options === 'function' ? options : callback);
27142714
options = getOptions(options, {});
2715-
if (!prefix || typeof prefix !== 'string') {
2716-
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
2717-
}
2715+
2716+
validateString(prefix, 'prefix');
27182717
nullCheck(prefix, 'prefix');
27192718
warnOnNonPortableTemplate(prefix);
27202719
const req = new FSReqCallback();
@@ -2730,9 +2729,8 @@ function mkdtemp(prefix, options, callback) {
27302729
*/
27312730
function mkdtempSync(prefix, options) {
27322731
options = getOptions(options, {});
2733-
if (!prefix || typeof prefix !== 'string') {
2734-
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
2735-
}
2732+
2733+
validateString(prefix, 'prefix');
27362734
nullCheck(prefix, 'prefix');
27372735
warnOnNonPortableTemplate(prefix);
27382736
const path = `${prefix}XXXXXX`;

lib/internal/fs/promises.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const { Buffer } = require('buffer');
2929
const {
3030
codes: {
3131
ERR_FS_FILE_TOO_LARGE,
32-
ERR_INVALID_ARG_TYPE,
3332
ERR_INVALID_ARG_VALUE,
3433
ERR_METHOD_NOT_IMPLEMENTED,
3534
},
@@ -74,6 +73,7 @@ const {
7473
validateBuffer,
7574
validateEncoding,
7675
validateInteger,
76+
validateString,
7777
} = require('internal/validators');
7878
const pathModule = require('path');
7979
const { promisify } = require('internal/util');
@@ -708,9 +708,8 @@ async function realpath(path, options) {
708708

709709
async function mkdtemp(prefix, options) {
710710
options = getOptions(options, {});
711-
if (!prefix || typeof prefix !== 'string') {
712-
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
713-
}
711+
712+
validateString(prefix, 'prefix');
714713
nullCheck(prefix);
715714
warnOnNonPortableTemplate(prefix);
716715
return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, kUsePromises);

test/parallel/test-fs-mkdtemp-prefix-check.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
const assert = require('assert');
44
const fs = require('fs');
55

6-
const prefixValues = [undefined, null, 0, true, false, 1, ''];
6+
const prefixValues = [undefined, null, 0, true, false, 1];
77

88
function fail(value) {
99
assert.throws(

0 commit comments

Comments
 (0)