Skip to content

fs: fix writev UNKNOWN on large buffers - #65736

Open
themuuln wants to merge 1 commit into
nodejs:mainfrom
themuuln:fs-fix-writev-unknown-40779
Open

fs: fix writev UNKNOWN on large buffers#65736
themuuln wants to merge 1 commit into
nodejs:mainfrom
themuuln:fs-fix-writev-unknown-40779

Conversation

@themuuln

@themuuln themuuln commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #40779

What changed

fs.writev, fs.writevSync, and FileHandle.writev now validate total byteLength before the syscall. If the sum exceeds kIoMaxLength (2147483647), they throw ERR_OUT_OF_RANGE with the received length instead of letting libuv wrap to -2147483648 and surface as UNKNOWN.

  • lib/fs.js: added loop + ERR_OUT_OF_RANGE.HideStackFramesError in writev and writevSync
  • lib/internal/fs/promises.js: same check in FileHandle.writev

This matches the fix for fs.write in c4e7dca, which validated length against kIoMaxLength before uv_fs_write.

Why

Repro from the issue:

const fd = fs.openSync("./test.dat", fs.constants.O_WRONLY | fs.constants.O_CREAT);
fs.writevSync(fd, [Buffer.alloc(0x7FFFFFFF + 1)], 0);
// before: throws { code: 'UNKNOWN', errno: -2147483648 }
// after: throws { code: 'ERR_OUT_OF_RANGE', message: 'The value of "length" is out of range. It must be <= 2147483647. Received 2147483648' }

Several small buffers that sum past 2 GiB hit the same wrap. The new check catches the sum early and gives an actionable error.

Tests

Added test/parallel/test-fs-writev-buffer-large.js, modeled on test-fs-write-buffer-large.js:

  • writevSync throws ERR_OUT_OF_RANGE for [bigBuffer] and [bigBuffer, small]
  • writev (callback) throws synchronously, callback never called
  • FileHandle.writev rejects with ERR_OUT_OF_RANGE; [] still returns bytesWritten: 0

The test skips on 32-bit and when allocation fails (Array buffer allocation failed).

Verification locally:

  • npx eslint lib/fs.js lib/internal/fs/promises.js test/parallel/test-fs-writev-buffer-large.js — no errors
  • Inspected kIoMaxLength imports and validateBufferArray order

Refs: c4e7dca

Check total byteLength across all buffers before the syscall.
A total over kIoMaxLength (2147483647) wraps to -2147483648
inside libuv and surfaces as UNKNOWN. Validate first and throw
ERR_OUT_OF_RANGE so the error is actionable.

Matches the earlier fix for fs.write in c4e7dca, which
validated length against INT32_MAX before uv_fs_write.

Fixes: nodejs#40779
Refs: nodejs@c4e7dca8f30
Signed-off-by: Temuulen Undrakhbayar <zerone.offical@gmail.com>
Assisted-by: Muse Spark
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. labels Sep 2, 2026
@codecov

codecov Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.01%. Comparing base (f9ab994) to head (249b825).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65736      +/-   ##
==========================================
- Coverage   90.07%   90.01%   -0.07%     
==========================================
  Files         754      755       +1     
  Lines      256395   257310     +915     
  Branches    48494    48784     +290     
==========================================
+ Hits       230947   231611     +664     
- Misses      16563    16793     +230     
- Partials     8885     8906      +21     
Files with missing lines Coverage Δ
lib/fs.js 98.44% <100.00%> (+<0.01%) ⬆️
lib/internal/fs/promises.js 92.32% <100.00%> (+0.02%) ⬆️

... and 44 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nonsense UNKNOWN error when writing large buffers with fs.writev

2 participants