GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fs: fix crash on negative zero file descriptor · nodejs/node@a2fad5d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • fs.js
  • test/parallel
    • test-fs-negative-zero.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/fs.js‎

Copy file name to clipboardExpand all lines: lib/fs.js
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,11 @@ function readFileSync(path, options) {
477477

478478
if ((options.encoding === 'utf8' || options.encoding === 'utf-8') &&
479479
!hasUserBuffer) {
480-
if (!isInt32(path)) {
480+
if (isInt32(path)) {
481+
// V8 does not report -0 as an int32, so it would reach the binding as a
482+
// path instead of a file descriptor.
483+
path |= 0;
484+
} else {
481485
path = getValidatedPath(path);
482486
}
483487
return binding.readFileUtf8(path, stringToFlags(options.flag));
@@ -2475,7 +2479,11 @@ function writeFileSync(path, data, options) {
24752479

24762480
// C++ fast path for string data and UTF8 encoding
24772481
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
2478-
if (!isInt32(path)) {
2482+
if (isInt32(path)) {
2483+
// V8 does not report -0 as an int32, so it would reach the binding as a
2484+
// path instead of a file descriptor.
2485+
path |= 0;
2486+
} else {
24792487
path = getValidatedPath(path);
24802488
}
24812489

Collapse file

‎test/parallel/test-fs-negative-zero.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-negative-zero.js
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require('../common');
44

5+
const assert = require('assert');
6+
const { spawnSync } = require('child_process');
57
const fs = require('fs');
68
const path = require('path');
79
const os = require('os');
@@ -29,5 +31,18 @@ ignoreExpectedError(() => fs.mkdirSync(missing, { mode: -0 }));
2931
ignoreExpectedError(() => fs.chmodSync(missing, -0));
3032
ignoreExpectedError(() => fs.writeFileSync(missing, '', { mode: -0 }));
3133

34+
// -0 is accepted as file descriptor 0. Writing an empty string reaches the
35+
// utf8 fast path without issuing a write on the descriptor.
36+
fs.writeFileSync(-0, '');
37+
fs.appendFileSync(-0, '');
38+
39+
const child = spawnSync(
40+
process.execPath,
41+
['-e', 'process.stdout.write(require("fs").readFileSync(-0, "utf8"))'],
42+
{ input: 'hello' },
43+
);
44+
assert.strictEqual(child.status, 0);
45+
assert.strictEqual(child.stdout.toString(), 'hello');
46+
3247
fs.watchFile(missing, { interval: -0 }, () => {});
3348
fs.unwatchFile(missing);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.