Skip to content

fs: fix abort on negative zero write length - #67524

Open
christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:fs-write-length-negative-zero
Open

christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:fs-write-length-negative-zero

Conversation

@christianaurichzm

Copy link
Copy Markdown
Member

fs.write(), fs.writeSync() and filehandle.write() abort the process when the length is -0, either positional or as options.length. This also happens on v22.23.1:

$ node -e "fs.writeSync(1, Buffer.from('abc'), 0, -0)"

  #  node[111331]: void node::fs::WriteBuffer(const v8::FunctionCallbackInfo<v8::Value>&) at ../src/node_file.cc:2233
  #  Assertion failed: args[3]->IsInt32()

validateOffsetLengthWrite() accepts -0, but V8 does not report -0 as an Int32, which the binding requires. This coerces the validated length to an int32 before entering the binding, as fs.read() already does, so -0 writes nothing, like 0.

The new block in test/parallel/test-fs-negative-zero.js aborts on main and passes with this change.

Refs: #65888

@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 Nov 16, 2026
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.73%. Comparing base (5884b8e) to head (557b254).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67524      +/-   ##
==========================================
+ Coverage   90.71%   90.73%   +0.01%     
==========================================
  Files         801      801              
  Lines      280906   280912       +6     
  Branches    54249    54243       -6     
==========================================
+ Hits       254832   254878      +46     
+ Misses      16246    16208      -38     
+ Partials     9828     9826       -2     
Files with missing lines Coverage Δ
lib/fs.js 97.11% <100.00%> (+<0.01%) ⬆️
lib/internal/fs/promises.js 91.85% <100.00%> (+0.04%) ⬆️

... and 30 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.

@vedchaudhari

Copy link
Copy Markdown
Contributor

Reproduced the crash on main, and on v22, v24 and v26 too. I also tried -0 in the other fs calls (offset, position, read, writev, truncate, streams) and none of them crash, so this PR fixes the only place that still did. LGTM, thanks!

@jasnell jasnell added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 18, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 18, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@christianaurichzm christianaurichzm added author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue PRs queued for automated landing through the Commit Queue. labels Nov 18, 2026
@nodejs-github-bot nodejs-github-bot added lacks-second-approval Commit Queue PRs awaiting a second collaborator approval or completion of the required wait. commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. and removed commit-queue PRs queued for automated landing through the Commit Queue. lacks-second-approval Commit Queue PRs awaiting a second collaborator approval or completion of the required wait. labels Nov 19, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Commit Queue failed

   ✖  This PR needs to wait 84 more hours to land (or 0 minutes if there is one more approval)
   ⚠  This PR has conflicts that must be resolved

The pull request was removed from the Commit Queue and labeled commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. . After resolving the failure, remove that label and add commit-queue PRs queued for automated landing through the Commit Queue. to retry.

Full Commit Queue output
�[36m⠋�[39m Loading data for nodejs/node/pull/67524
�[36m⠋�[39m Loading data for nodejs/node/pull/67524
�[36m⠋�[39m Getting collaborator contacts from README of nodejs/node
�[36m⠋�[39m Getting PR from nodejs/node/pull/67524
�[36m⠋�[39m Getting reviews from nodejs/node/pull/67524
�[36m⠋�[39m Getting comments from nodejs/node/pull/67524
�[36m⠋�[39m Getting commits from nodejs/node/pull/67524
✔  Done loading data for nodejs/node/pull/67524
----------------------------------- PR info ------------------------------------
Title      fs: fix abort on negative zero write length (#67524)
Author     Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com> (@christianaurichzm)
Branch     christianaurichzm:fs-write-length-negative-zero -> nodejs:main
Labels     fs, author ready, needs-ci, commit-queue, lacks-second-approval
Commits    1
 - fs: fix abort on negative zero write length
Committers 1
 - Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67524
Refs: https://github.com/nodejs/node/pull/65888
Reviewed-By: James M Snell <jasnell@gmail.com>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/67524
Refs: https://github.com/nodejs/node/pull/65888
Reviewed-By: James M Snell <jasnell@gmail.com>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Mon, 16 Nov 2026 16:57:22 GMT
   ✔  Approvals: 1
   ✔  - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/67524#pullrequestreview-5956474711
   ✖  This PR needs to wait 84 more hours to land (or 0 minutes if there is one more approval)
   ⚠  This PR has conflicts that must be resolved
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2026-11-18T12:31:35Z: https://ci.nodejs.org/job/node-test-pull-request/79253/
�[36m⠙�[39m Querying data for job/node-test-pull-request/79253/
�[36m⠙�[39m Querying data for job/node-test-pull-request/79253/
�[36m⠙�[39m Querying API for job/node-test-pull-request/79253/
✔  Build data downloaded
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu

View workflow run

`validateOffsetLengthWrite()` accepts -0 as a length, but V8 does not
represent -0 as an Int32 value, so the `IsInt32()` check in the binding
aborted the process in `fs.write()`, `fs.writeSync()` and
`filehandle.write()`.

Coerce the validated length to an int32 before the call, as `fs.read()`
already does.

Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
@christianaurichzm
christianaurichzm force-pushed the fs-write-length-negative-zero branch from a25fb60 to 557b254 Compare November 20, 2026 09:47
@christianaurichzm christianaurichzm removed commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. author ready PRs with CI started, the required approvals, and no outstanding review comments. labels Nov 20, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@christianaurichzm

Copy link
Copy Markdown
Member Author

Rebased to resolve the conflict with #67567, which changed the top of the same test. Only the test changed. @jasnell @LiviaMedeiros could you take a look?

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.

5 participants