Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: refactor to avoid unsafe array iteration #36699

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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