GeezerVison is showing github.com — fs: fix crash on negative zero file descriptor · nodejs/node@a2fad5d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +10-2Lines changed: 10 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -477,7 +477,11 @@ function readFileSync(path, options) { | 477 | 477 |
| 478 | 478 | if ((options.encoding === 'utf8' || options.encoding === 'utf-8') && | 479 | 479 | !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 { | 481 | 485 | path = getValidatedPath(path); | 482 | 486 | } | 483 | 487 | return binding.readFileUtf8(path, stringToFlags(options.flag)); | @@ -2475,7 +2479,11 @@ function writeFileSync(path, data, options) { | 2475 | 2479 |
| 2476 | 2480 | // C++ fast path for string data and UTF8 encoding | 2477 | 2481 | 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 { | 2479 | 2487 | path = getValidatedPath(path); | 2480 | 2488 | } | 2481 | 2489 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-negative-zero.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | require('../common'); | 4 | 4 |
| | 5 | +const assert = require('assert'); | | 6 | +const { spawnSync } = require('child_process'); | 5 | 7 | const fs = require('fs'); | 6 | 8 | const path = require('path'); | 7 | 9 | const os = require('os'); | @@ -29,5 +31,18 @@ ignoreExpectedError(() => fs.mkdirSync(missing, { mode: -0 })); | 29 | 31 | ignoreExpectedError(() => fs.chmodSync(missing, -0)); | 30 | 32 | ignoreExpectedError(() => fs.writeFileSync(missing, '', { mode: -0 })); | 31 | 33 |
| | 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 | + | 32 | 47 | fs.watchFile(missing, { interval: -0 }, () => {}); | 33 | 48 | fs.unwatchFile(missing); |
|