GeezerVison is showing github.com — fs: fix abort on negative zero write length · nodejs/node@ee6fd3b
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 +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1120,6 +1120,8 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) { | 1120 | 1120 | if (typeof position !== 'number') | 1121 | 1121 | position = null; | 1122 | 1122 | validateOffsetLengthWrite(offset, length, buffer.byteLength); | | 1123 | + // V8 does not report -0 as an int32, which the binding requires. | | 1124 | + length |= 0; | 1123 | 1125 |
| 1124 | 1126 | const h = vfsState.handlers; | 1125 | 1127 | if (h !== null) { | @@ -1210,6 +1212,8 @@ function writeSync(fd, buffer, offsetOrOptions, length, position) { | 1210 | 1212 | if (typeof length !== 'number') | 1211 | 1213 | length = buffer.byteLength - offset; | 1212 | 1214 | validateOffsetLengthWrite(offset, length, buffer.byteLength); | | 1215 | + // V8 does not report -0 as an int32, which the binding requires. | | 1216 | + length |= 0; | 1213 | 1217 |
| 1214 | 1218 | const h = vfsState.handlers; | 1215 | 1219 | if (h !== null) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1576,6 +1576,8 @@ async function write(handle, buffer, offsetOrOptions, length, position) { | 1576 | 1576 | if (typeof position !== 'number') | 1577 | 1577 | position = null; | 1578 | 1578 | validateOffsetLengthWrite(offset, length, buffer.byteLength); | | 1579 | + // V8 does not report -0 as an int32, which the binding requires. | | 1580 | + length |= 0; | 1579 | 1581 | const bytesWritten = | 1580 | 1582 | (await PromisePrototypeThen( | 1581 | 1583 | binding.writeBuffer(handle.fd, buffer, offset, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-negative-zero.js +26-1Lines changed: 26 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -require('../common'); | | 3 | +const common = require('../common'); | 4 | 4 | const tmpdir = require('../common/tmpdir'); | 5 | 5 | const assert = require('assert'); | 6 | 6 | const { spawnSync } = require('child_process'); | @@ -45,3 +45,28 @@ assert.strictEqual(child.stdout.toString(), 'hello'); | 45 | 45 |
| 46 | 46 | fs.watchFile(missing, { interval: -0 }, () => {}); | 47 | 47 | fs.unwatchFile(missing); | | 48 | + | | 49 | +// A length of -0 writes nothing, like 0. | | 50 | +{ | | 51 | + const buffer = Buffer.from('abc'); | | 52 | + | | 53 | + const fd = fs.openSync(tmpdir.resolve('negative-zero-fd.txt'), 'w'); | | 54 | + assert.strictEqual(fs.writeSync(fd, buffer, 0, -0), 0); | | 55 | + assert.strictEqual(fs.writeSync(fd, buffer, { length: -0 }), 0); | | 56 | + fs.write(fd, buffer, 0, -0, common.mustSucceed((written) => { | | 57 | + assert.strictEqual(written, 0); | | 58 | + fs.write(fd, buffer, { length: -0 }, common.mustSucceed((written) => { | | 59 | + assert.strictEqual(written, 0); | | 60 | + fs.closeSync(fd); | | 61 | + })); | | 62 | + })); | | 63 | + | | 64 | + (async () => { | | 65 | + const handle = | | 66 | + await fs.promises.open(tmpdir.resolve('negative-zero-handle.txt'), 'w'); | | 67 | + assert.strictEqual((await handle.write(buffer, 0, -0)).bytesWritten, 0); | | 68 | + assert.strictEqual( | | 69 | + (await handle.write(buffer, { length: -0 })).bytesWritten, 0); | | 70 | + await handle.close(); | | 71 | + })().then(common.mustCall()); | | 72 | +} |
|