Skip to content

Commit 593b5bd

Browse files
committed
fs: fix do_not_throw_error argument index in sync fstat
The synchronous branch of the fstat binding read its do_not_throw_error flag from args[2], which is always undefined on that branch, so the flag was ignored and the binding always threw. Read the flag from args[3] as documented, and restore tryStatSync() to closing the file descriptor it owns before rethrowing the fstat error, which was its behaviour before the flag was introduced. Refs: #49868 Assisted-by: MiniMax M3.1 Flash Preview + Opus 5.5 Signed-off-by: Daniel Lockyer <hi@daniellockyer.com>
1 parent ae5a0f4 commit 593b5bd

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎lib/fs.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,13 @@ function readFileAfterOneShot(err, buffer, fd, size, closeErr) {
516516
}
517517

518518
function tryStatSync(fd, isUserFd) {
519-
const stats = binding.fstat(fd, false, undefined, true /* shouldNotThrow */);
520-
if (stats === undefined && !isUserFd) {
521-
fs.closeSync(fd);
519+
let threw = true;
520+
let stats;
521+
try {
522+
stats = binding.fstat(fd, false, undefined, false);
523+
threw = false;
524+
} finally {
525+
if (threw && !isUserFd) fs.closeSync(fd);
522526
}
523527
return stats;
524528
}

‎src/node_file.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ static void FStat(const FunctionCallbackInfo<Value>& args) {
12871287
AsyncCall(
12881288
env, req_wrap_async, args, "fstat", UTF8, AfterStat, uv_fs_fstat, fd);
12891289
} else { // fstat(fd, use_bigint, undefined, do_not_throw_error)
1290-
bool do_not_throw_error = args[2]->IsTrue();
1290+
bool do_not_throw_error = args[3]->IsTrue();
12911291
const auto should_throw = [do_not_throw_error](int result) {
12921292
return is_uv_error(result) && !do_not_throw_error;
12931293
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const fs = require('fs');
7+
const { internalBinding } = require('internal/test/binding');
8+
9+
const binding = internalBinding('fs');
10+
11+
const badFd = fs.openSync(__filename, 'r');
12+
fs.closeSync(badFd);
13+
14+
// The synchronous fstat binding honours its do_not_throw_error argument.
15+
assert.strictEqual(binding.fstat(badFd, false, undefined, true), undefined);
16+
assert.throws(() => binding.fstat(badFd, false, undefined, false), {
17+
code: 'EBADF',
18+
syscall: 'fstat',
19+
});
20+
21+
// readFileSync() surfaces the fstat error and closes a file descriptor it
22+
// opened itself, but leaves a user-supplied one alone.
23+
fs.openSync = () => badFd;
24+
fs.closeSync = common.mustCall((fd) => {
25+
assert.strictEqual(fd, badFd);
26+
});
27+
28+
assert.throws(() => fs.readFileSync('dummy', 'latin1'), {
29+
code: 'EBADF',
30+
syscall: 'fstat',
31+
});
32+
assert.throws(() => fs.readFileSync(badFd, 'latin1'), {
33+
code: 'EBADF',
34+
syscall: 'fstat',
35+
});

0 commit comments

Comments
 (0)