Skip to content

Commit e3a091d

Browse files
aduh95danielleadams
authored andcommitted
fs: refactor to avoid unsafe array iteration
PR-URL: #36699 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 302be57 commit e3a091d

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

lib/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function maybeCallback(cb) {
181181
function makeCallback(cb) {
182182
validateCallback(cb);
183183

184-
return (...args) => cb(...args);
184+
return (...args) => ReflectApply(cb, this, args);
185185
}
186186

187187
// Special case of `makeCallback()` that is specific to async `*stat()` calls as

lib/internal/fs/dir.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Dir {
9999
}
100100

101101
if (this[kDirBufferedEntries].length > 0) {
102-
const [ name, type ] =
102+
const { 0: name, 1: type } =
103103
ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
104104
if (maybeSync)
105105
process.nextTick(getDirent, this[kDirPath], name, type, callback);
@@ -142,7 +142,7 @@ class Dir {
142142
}
143143

144144
if (this[kDirBufferedEntries].length > 0) {
145-
const [ name, type ] =
145+
const { 0: name, 1: type } =
146146
ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
147147
return getDirent(this[kDirPath], name, type);
148148
}
@@ -182,7 +182,7 @@ class Dir {
182182
}
183183

184184
if (this[kDirOperationQueue] !== null) {
185-
this[kDirOperationQueue].push(() => {
185+
ArrayPrototypePush(this[kDirOperationQueue], () => {
186186
this.close(callback);
187187
});
188188
return;

lib/internal/fs/promises.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
PromisePrototypeFinally,
2020
PromisePrototypeThen,
2121
PromiseResolve,
22+
SafeArrayIterator,
2223
Symbol,
2324
Uint8Array,
2425
} = primordials;
@@ -263,7 +264,7 @@ async function fsCall(fn, handle, ...args) {
263264

264265
try {
265266
handle[kRef]();
266-
return await fn(handle, ...args);
267+
return await fn(handle, ...new SafeArrayIterator(args));
267268
} finally {
268269
handle[kUnref]();
269270
}

lib/internal/fs/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ function join(path, name) {
217217
'path', ['string', 'Buffer'], path);
218218
}
219219

220-
function getDirents(path, [names, types], callback) {
220+
function getDirents(path, { 0: names, 1: types }, callback) {
221221
let i;
222222
if (typeof callback === 'function') {
223223
const len = names.length;

0 commit comments

Comments
 (0)