| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 28bca33 commit 8e76397
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -133,6 +133,7 @@ const { | |||
| 133 | 133 | validateBoolean, | |
| 134 | 134 | validateBuffer, | |
| 135 | 135 | validateCallback, | |
| 136 | + validateEncoding, | ||
| 136 | 137 | validateFunction, | |
| 137 | 138 | validateInteger, | |
| 138 | 139 | } = require('internal/validators'); | |
@@ -702,11 +703,14 @@ function write(fd, buffer, offset, length, position, callback) { | |||
| 702 | 703 | } | |
| 703 | 704 | length = 'utf8'; | |
| 704 | 705 | } | |
| 706 | + | ||
| 707 | + const str = String(buffer); | ||
| 708 | + validateEncoding(str, length); | ||
| 705 | 709 | callback = maybeCallback(position); | |
| 706 | 710 | ||
| 707 | 711 | const req = new FSReqCallback(); | |
| 708 | 712 | req.oncomplete = wrapper; | |
| 709 | - return binding.writeString(fd, String(buffer), offset, length, req); | ||
| 713 | + return binding.writeString(fd, str, offset, length, req); | ||
| 710 | 714 | } | |
| 711 | 715 | ||
| 712 | 716 | ObjectDefineProperty(write, internalUtil.customPromisifyArgs, | |
@@ -735,6 +739,7 @@ function writeSync(fd, buffer, offset, length, position) { | |||
| 735 | 739 | undefined, ctx); | |
| 736 | 740 | } else { | |
| 737 | 741 | validateStringAfterArrayBufferView(buffer, 'buffer'); | |
| 742 | + validateEncoding(buffer, length); | ||
| 738 | 743 | ||
| 739 | 744 | if (offset === undefined) | |
| 740 | 745 | offset = null; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,6 +70,7 @@ const { | |||
| 70 | 70 | validateAbortSignal, | |
| 71 | 71 | validateBoolean, | |
| 72 | 72 | validateBuffer, | |
| 73 | + validateEncoding, | ||
| 73 | 74 | validateInteger, | |
| 74 | 75 | } = require('internal/validators'); | |
| 75 | 76 | const pathModule = require('path'); | |
@@ -467,6 +468,7 @@ async function write(handle, buffer, offset, length, position) { | |||
| 467 | 468 | } | |
| 468 | 469 | ||
| 469 | 470 | validateStringAfterArrayBufferView(buffer, 'buffer'); | |
| 471 | + validateEncoding(buffer, length); | ||
| 470 | 472 | const bytesWritten = (await binding.writeString(handle.fd, buffer, offset, | |
| 471 | 473 | length, kUsePromises)) || 0; | |
| 472 | 474 | return { bytesWritten, buffer }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -436,6 +436,22 @@ async function getHandle(dest) { | |||
| 436 | 436 | ); | |
| 437 | 437 | } | |
| 438 | 438 | ||
| 439 | + // Regression test for https://github.com/nodejs/node/issues/38168 | ||
| 440 | + { | ||
| 441 | + const handle = await getHandle(dest); | ||
| 442 | + | ||
| 443 | + assert.rejects( | ||
| 444 | + async () => handle.write('abc', 0, 'hex'), | ||
| 445 | + { | ||
| 446 | + code: 'ERR_INVALID_ARG_VALUE', | ||
| 447 | + message: /'encoding' is invalid for data of length 3/ | ||
| 448 | + } | ||
| 449 | + ); | ||
| 450 | + | ||
| 451 | + const ret = await handle.write('abcd', 0, 'hex'); | ||
| 452 | + assert.strictEqual(ret.bytesWritten, 2); | ||
| 453 | + await handle.close(); | ||
| 454 | + } | ||
| 439 | 455 | } | |
| 440 | 456 | ||
| 441 | 457 | doTest().then(common.mustCall()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,6 +33,7 @@ const fn = path.join(tmpdir.path, 'write.txt'); | |||
| 33 | 33 | const fn2 = path.join(tmpdir.path, 'write2.txt'); | |
| 34 | 34 | const fn3 = path.join(tmpdir.path, 'write3.txt'); | |
| 35 | 35 | const fn4 = path.join(tmpdir.path, 'write4.txt'); | |
| 36 | + const fn5 = path.join(tmpdir.path, 'write5.txt'); | ||
| 36 | 37 | const expected = 'ümlaut.'; | |
| 37 | 38 | const constants = fs.constants; | |
| 38 | 39 | ||
@@ -170,3 +171,31 @@ fs.open(fn4, 'w', 0o644, common.mustSucceed((fd) => { | |||
| 170 | 171 | } | |
| 171 | 172 | ); | |
| 172 | 173 | }); | |
| 174 | + | ||
| 175 | + { | ||
| 176 | + // Regression test for https://github.com/nodejs/node/issues/38168 | ||
| 177 | + const fd = fs.openSync(fn5, 'w'); | ||
| 178 | + | ||
| 179 | + assert.throws( | ||
| 180 | + () => fs.writeSync(fd, 'abc', 0, 'hex'), | ||
| 181 | + { | ||
| 182 | + code: 'ERR_INVALID_ARG_VALUE', | ||
| 183 | + message: /'encoding' is invalid for data of length 3/ | ||
| 184 | + } | ||
| 185 | + ); | ||
| 186 | + | ||
| 187 | + assert.throws( | ||
| 188 | + () => fs.writeSync(fd, 'abc', 0, 'hex', common.mustNotCall()), | ||
| 189 | + { | ||
| 190 | + code: 'ERR_INVALID_ARG_VALUE', | ||
| 191 | + message: /'encoding' is invalid for data of length 3/ | ||
| 192 | + } | ||
| 193 | + ); | ||
| 194 | + | ||
| 195 | + assert.strictEqual(fs.writeSync(fd, 'abcd', 0, 'hex'), 2); | ||
| 196 | + | ||
| 197 | + fs.write(fd, 'abcd', 0, 'hex', common.mustSucceed((written) => { | ||
| 198 | + assert.strictEqual(written, 2); | ||
| 199 | + fs.closeSync(fd); | ||
| 200 | + })); | ||
| 201 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments