From 7697ce031043e82707bffac670159ce9bd46bbc8 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Fri, 16 Jan 2026 14:45:18 +0300 Subject: [PATCH] fs: remove duplicate fd validation in sync functions PR-URL: https://github.com/nodejs/node/pull/61361 Reviewed-By: Aviv Keller Reviewed-By: Xuguang Mei --- lib/fs.js | 7 ------- src/node_file.cc | 30 ++++++++++++++++++++---------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index ace5c464b73..1c65c01c939 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -597,7 +597,6 @@ function openAsBlob(path, options = kEmptyObject) { */ function read(fd, buffer, offsetOrOptions, length, position, callback) { fd = getValidatedFd(fd); - let offset = offsetOrOptions; let params = null; if (arguments.length <= 4) { @@ -689,8 +688,6 @@ ObjectDefineProperty(read, kCustomPromisifyArgsSymbol, * @returns {number} */ function readSync(fd, buffer, offsetOrOptions, length, position) { - fd = getValidatedFd(fd); - validateBuffer(buffer); let offset = offsetOrOptions; @@ -779,7 +776,6 @@ ObjectDefineProperty(readv, kCustomPromisifyArgsSymbol, * @returns {number} */ function readvSync(fd, buffers, position) { - fd = getValidatedFd(fd); validateBufferArray(buffers); if (typeof position !== 'number') @@ -809,7 +805,6 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) { } fd = getValidatedFd(fd); - let offset = offsetOrOptions; if (isArrayBufferView(buffer)) { callback ||= position || length || offset; @@ -880,7 +875,6 @@ ObjectDefineProperty(write, kCustomPromisifyArgsSymbol, * @returns {number} */ function writeSync(fd, buffer, offsetOrOptions, length, position) { - fd = getValidatedFd(fd); const ctx = {}; let result; @@ -970,7 +964,6 @@ ObjectDefineProperty(writev, kCustomPromisifyArgsSymbol, { * @returns {number} */ function writevSync(fd, buffers, position) { - fd = getValidatedFd(fd); validateBufferArray(buffers); if (buffers.length === 0) { diff --git a/src/node_file.cc b/src/node_file.cc index 15e0ff923bc..9015dbc4a9c 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -2314,8 +2314,10 @@ static void WriteBuffer(const FunctionCallbackInfo& args) { const int argc = args.Length(); CHECK_GE(argc, 4); - CHECK(args[0]->IsInt32()); - const int fd = args[0].As()->Value(); + int fd; + if (!GetValidatedFd(env, args[0]).To(&fd)) { + return; + } CHECK(Buffer::HasInstance(args[1])); Local buffer_obj = args[1].As(); @@ -2381,8 +2383,10 @@ static void WriteBuffers(const FunctionCallbackInfo& args) { const int argc = args.Length(); CHECK_GE(argc, 3); - CHECK(args[0]->IsInt32()); - const int fd = args[0].As()->Value(); + int fd; + if (!GetValidatedFd(env, args[0]).To(&fd)) { + return; + } CHECK(args[1]->IsArray()); Local chunks = args[1].As(); @@ -2441,8 +2445,10 @@ static void WriteString(const FunctionCallbackInfo& args) { const int argc = args.Length(); CHECK_GE(argc, 4); - CHECK(args[0]->IsInt32()); - const int fd = args[0].As()->Value(); + int fd; + if (!GetValidatedFd(env, args[0]).To(&fd)) { + return; + } const int64_t pos = GetOffset(args[2]); @@ -2634,8 +2640,10 @@ static void Read(const FunctionCallbackInfo& args) { const int argc = args.Length(); CHECK_GE(argc, 5); - CHECK(args[0]->IsInt32()); - const int fd = args[0].As()->Value(); + int fd; + if (!GetValidatedFd(env, args[0]).To(&fd)) { + return; + } CHECK(Buffer::HasInstance(args[1])); Local buffer_obj = args[1].As(); @@ -2765,8 +2773,10 @@ static void ReadBuffers(const FunctionCallbackInfo& args) { const int argc = args.Length(); CHECK_GE(argc, 3); - CHECK(args[0]->IsInt32()); - const int fd = args[0].As()->Value(); + int fd; + if (!GetValidatedFd(env, args[0]).To(&fd)) { + return; + } CHECK(args[1]->IsArray()); Local buffers = args[1].As();