nodejs / nodejs/node

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

Đang mở
#64,367 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

fs
Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.3k
Merge trung bình
4 ngày 2 giờ
Pull request đã merge (30 ngày)
283

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy bản tái hiện JavaScript được cung cấp trên Node.js, sau đó kiểm tra lib/internal/fs/read/context.js quanh các dòng 180-194, đặc biệt là ReadFileContext.close(err) đối với các bộ mô tả tệp do người dùng cung cấp. Được xem là hoàn tất khi lệnh gọi với fd dạng số và bộ đệm không đủ kích thước báo cáo ERR_INVALID_ARG_VALUE thay vì hoàn tất thành công.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, node.js
Lĩnh vực
operating-systems
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
74/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.