Skip to content

Commit

Permalink
fs: fix realpath inode link caching
Browse files Browse the repository at this point in the history
The `fs.realpath` / `fs.realpathSync` cache already seen symbolic links
using the inode number which may be longer that max supported
JS number (2**53) and will therefore be incorrectly handled by possibly
entering infinite loop of calling stat on the same node.

This PR changes those functions (where appropriate) to use
bigint for inode numbers.

Fixes: #33936

PR-URL: #33945
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
lundibundi authored and codebytere committed Jul 12, 2020
1 parent 2e38f0d commit b92c0cb
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
const {
Map,
MathMax,
Number,
NumberIsSafeInteger,
ObjectCreate,
ObjectDefineProperties,
Expand Down Expand Up @@ -174,7 +175,10 @@ const isFd = isUint32;
function isFileType(stats, fileType) {
// Use stats array directly to avoid creating an fs.Stats instance just for
// our internal use.
return (stats[1/* mode */] & S_IFMT) === fileType;
let mode = stats[1];
if (typeof mode === 'bigint')
mode = Number(mode);
return (mode & S_IFMT) === fileType;
}

function access(path, mode, callback) {
Expand Down Expand Up @@ -1614,7 +1618,7 @@ function realpathSync(p, options) {

const baseLong = pathModule.toNamespacedPath(base);
const ctx = { path: base };
const stats = binding.lstat(baseLong, false, undefined, ctx);
const stats = binding.lstat(baseLong, true, undefined, ctx);
handleErrorFromBinding(ctx);

if (!isFileType(stats, S_IFLNK)) {
Expand Down Expand Up @@ -1747,7 +1751,7 @@ function realpath(p, options, callback) {
return process.nextTick(LOOP);
}

return fs.lstat(base, gotStat);
return fs.lstat(base, { bigint: true }, gotStat);
}

function gotStat(err, stats) {
Expand Down

0 comments on commit b92c0cb

Please sign in to comment.