Skip to content

Commit

Permalink
fs: refactor to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
PR-URL: #36699
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 authored and danielleadams committed Jan 12, 2021
1 parent 302be57 commit e3a091d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function maybeCallback(cb) {
function makeCallback(cb) {
validateCallback(cb);

return (...args) => cb(...args);
return (...args) => ReflectApply(cb, this, args);
}

// Special case of `makeCallback()` that is specific to async `*stat()` calls as
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Dir {
}

if (this[kDirBufferedEntries].length > 0) {
const [ name, type ] =
const { 0: name, 1: type } =
ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
if (maybeSync)
process.nextTick(getDirent, this[kDirPath], name, type, callback);
Expand Down Expand Up @@ -142,7 +142,7 @@ class Dir {
}

if (this[kDirBufferedEntries].length > 0) {
const [ name, type ] =
const { 0: name, 1: type } =
ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
return getDirent(this[kDirPath], name, type);
}
Expand Down Expand Up @@ -182,7 +182,7 @@ class Dir {
}

if (this[kDirOperationQueue] !== null) {
this[kDirOperationQueue].push(() => {
ArrayPrototypePush(this[kDirOperationQueue], () => {
this.close(callback);
});
return;
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
PromisePrototypeFinally,
PromisePrototypeThen,
PromiseResolve,
SafeArrayIterator,
Symbol,
Uint8Array,
} = primordials;
Expand Down Expand Up @@ -263,7 +264,7 @@ async function fsCall(fn, handle, ...args) {

try {
handle[kRef]();
return await fn(handle, ...args);
return await fn(handle, ...new SafeArrayIterator(args));
} finally {
handle[kUnref]();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function join(path, name) {
'path', ['string', 'Buffer'], path);
}

function getDirents(path, [names, types], callback) {
function getDirents(path, { 0: names, 1: types }, callback) {
let i;
if (typeof callback === 'function') {
const len = names.length;
Expand Down

0 comments on commit e3a091d

Please sign in to comment.