nodejs / nodejs/node

fs: preserve readFile() errors when using a numeric fd and buffer option

未关闭
#64,367 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

fs
主要语言
JavaScript
星标
122k
派生
37.3k
平均合并
4 天 2 小时
30 天内合并 PR
283

描述

Version

v24.18.0

Platform
Windows 11 x64
Subsystem

fs

What steps will reproduce the bug?

'use strict';

const fs = require('node:fs');

fs.writeFileSync('repro.txt', 'Hello world');

fs.open('repro.txt', 'r', (err, fd) => {
if (err) throw err;

fs.readFile(fd, { buffer: Buffer.alloc(5) }, (err, data) => {
console.log('err:', err && err.code);
console.log('data:', data && data.toString());
fs.closeSync(fd);
});
});

How often does it reproduce? Is there a required condition?

Always, when fs.readFile() is called with:
a numeric file descriptor, and
options.buffer, and
the supplied buffer is too small for the file

What is the expected behavior? Why is that the expected behavior?

The call should fail with ERR_INVALID_ARG_VALUE because the provided buffer is too small to contain the full file contents.

What do you see instead?

The error is dropped for user-supplied file descriptors, and the callback can complete successfully instead of reporting ERR_INVALID_ARG_VALUE.

Additional information

Root cause appears to be in lib/internal/fs/read/context.js (lines 180-194): ReadFileContext.close(err) ignores err when isUserFd is true, so the final callback runs as if no failure occurred.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先在 Node.js 上运行提供的 JavaScript 复现代码,然后检查 lib/internal/fs/read/context.js 第 180-194 行附近的内容,尤其是针对用户文件描述符的 ReadFileContext.close(err)。当使用数值 fd 和大小不足的缓冲区进行调用时报告 ERR_INVALID_ARG_VALUE,而不是成功完成,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js
领域
operating-systems
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
描述清楚
新手友好度
74/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。