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@1e17275

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
@@ -613,7 +613,11 @@ function readFileSync(path, options) {
613613

614614
if ((options.encoding === 'utf8' || options.encoding === 'utf-8') &&
615615
!hasUserBuffer) {
616-
if (!isInt32(path)) {
616+
if (isInt32(path)) {
617+
// V8 does not report -0 as an int32, so it would reach the binding as a
618+
// path instead of a file descriptor.
619+
path |= 0;
620+
} else {
617621
path = getValidatedPath(path);
618622
}
619623
return binding.readFileUtf8(path, stringToFlags(options.flag));
@@ -3012,7 +3016,11 @@ function writeFileSync(path, data, options) {
30123016

30133017
// C++ fast path for string data and UTF8 encoding
30143018
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
3015-
if (!isInt32(path)) {
3019+
if (isInt32(path)) {
3020+
// V8 does not report -0 as an int32, so it would reach the binding as a
3021+
// path instead of a file descriptor.
3022+
path |= 0;
3023+
} else {
30163024
path = getValidatedPath(path);
30173025
}
30183026

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 01402792 — 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.