Skip to content

Commit b894df8

Browse files
lundibundicodebytere
authored andcommitted
fs: fix realpath inode link caching
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 <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent e1bebf1 commit b894df8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/fs.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const kIoMaxLength = 2 ** 31 - 1;
3131
const {
3232
Map,
3333
MathMax,
34+
Number,
3435
NumberIsSafeInteger,
3536
ObjectCreate,
3637
ObjectDefineProperties,
@@ -182,7 +183,10 @@ const isFd = isUint32;
182183
function isFileType(stats, fileType) {
183184
// Use stats array directly to avoid creating an fs.Stats instance just for
184185
// our internal use.
185-
return (stats[1/* mode */] & S_IFMT) === fileType;
186+
let mode = stats[1];
187+
if (typeof mode === 'bigint')
188+
mode = Number(mode);
189+
return (mode & S_IFMT) === fileType;
186190
}
187191

188192
function access(path, mode, callback) {
@@ -1672,7 +1676,7 @@ function realpathSync(p, options) {
16721676

16731677
const baseLong = pathModule.toNamespacedPath(base);
16741678
const ctx = { path: base };
1675-
const stats = binding.lstat(baseLong, false, undefined, ctx);
1679+
const stats = binding.lstat(baseLong, true, undefined, ctx);
16761680
handleErrorFromBinding(ctx);
16771681

16781682
if (!isFileType(stats, S_IFLNK)) {
@@ -1805,7 +1809,7 @@ function realpath(p, options, callback) {
18051809
return process.nextTick(LOOP);
18061810
}
18071811

1808-
return fs.lstat(base, gotStat);
1812+
return fs.lstat(base, { bigint: true }, gotStat);
18091813
}
18101814

18111815
function gotStat(err, stats) {

0 commit comments

Comments
 (0)