GeezerVison is showing github.com — fs: fix crash on negative zero file descriptor · nodejs/node@1e17275
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 |
|---|
@@ -613,7 +613,11 @@ function readFileSync(path, options) { | 613 | 613 |
| 614 | 614 | if ((options.encoding === 'utf8' || options.encoding === 'utf-8') && | 615 | 615 | !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 { | 617 | 621 | path = getValidatedPath(path); | 618 | 622 | } | 619 | 623 | return binding.readFileUtf8(path, stringToFlags(options.flag)); | @@ -3012,7 +3016,11 @@ function writeFileSync(path, data, options) { | 3012 | 3016 |
| 3013 | 3017 | // C++ fast path for string data and UTF8 encoding | 3014 | 3018 | 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 { | 3016 | 3024 | path = getValidatedPath(path); | 3017 | 3025 | } | 3018 | 3026 |
| |
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); |
|