GeezerVison is showing github.com — fs: validate position argument before length === 0 early return · nodejs/node@63c111c
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 +12-12Lines changed: 12 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -646,6 +646,12 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) { | 646 | 646 |
| 647 | 647 | length |= 0; | 648 | 648 |
| | 649 | + if (position == null) { | | 650 | + position = -1; | | 651 | + } else { | | 652 | + validatePosition(position, 'position', length); | | 653 | + } | | 654 | + | 649 | 655 | if (length === 0) { | 650 | 656 | return process.nextTick(function tick() { | 651 | 657 | callback(null, 0, buffer); | @@ -659,12 +665,6 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) { | 659 | 665 |
| 660 | 666 | validateOffsetLengthRead(offset, length, buffer.byteLength); | 661 | 667 |
| 662 | | - if (position == null) { | 663 | | - position = -1; | 664 | | - } else { | 665 | | - validatePosition(position, 'position', length); | 666 | | - } | 667 | | - | 668 | 668 | function wrapper(err, bytesRead) { | 669 | 669 | // Retain a reference to buffer so that it can't be GC'ed too soon. | 670 | 670 | callback(err, bytesRead || 0, buffer); | @@ -717,6 +717,12 @@ function readSync(fd, buffer, offsetOrOptions, length, position) { | 717 | 717 |
| 718 | 718 | length |= 0; | 719 | 719 |
| | 720 | + if (position == null) { | | 721 | + position = -1; | | 722 | + } else { | | 723 | + validatePosition(position, 'position', length); | | 724 | + } | | 725 | + | 720 | 726 | if (length === 0) { | 721 | 727 | return 0; | 722 | 728 | } | @@ -728,12 +734,6 @@ function readSync(fd, buffer, offsetOrOptions, length, position) { | 728 | 734 |
| 729 | 735 | validateOffsetLengthRead(offset, length, buffer.byteLength); | 730 | 736 |
| 731 | | - if (position == null) { | 732 | | - position = -1; | 733 | | - } else { | 734 | | - validatePosition(position, 'position', length); | 735 | | - } | 736 | | - | 737 | 737 | return binding.read(fd, buffer, offset, length, position); | 738 | 738 | } | 739 | 739 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -678,6 +678,12 @@ async function read(handle, bufferOrParams, offset, length, position) { | 678 | 678 |
| 679 | 679 | length ??= buffer.byteLength - offset; | 680 | 680 |
| | 681 | + if (position == null) { | | 682 | + position = -1; | | 683 | + } else { | | 684 | + validatePosition(position, 'position', length); | | 685 | + } | | 686 | + | 681 | 687 | if (length === 0) | 682 | 688 | return { __proto__: null, bytesRead: length, buffer }; | 683 | 689 |
| @@ -688,12 +694,6 @@ async function read(handle, bufferOrParams, offset, length, position) { | 688 | 694 |
| 689 | 695 | validateOffsetLengthRead(offset, length, buffer.byteLength); | 690 | 696 |
| 691 | | - if (position == null) { | 692 | | - position = -1; | 693 | | - } else { | 694 | | - validatePosition(position, 'position', length); | 695 | | - } | 696 | | - | 697 | 697 | const bytesRead = (await PromisePrototypeThen( | 698 | 698 | binding.read(handle.fd, buffer, offset, length, position, kUsePromises), | 699 | 699 | undefined, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-read-position-validation.mjs +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,3 +91,26 @@ async function testInvalid(code, position) { | 91 | 91 | await testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue); | 92 | 92 | } | 93 | 93 | } | | 94 | + | | 95 | +{ | | 96 | + const emptyBuffer = Buffer.alloc(0); | | 97 | + await new Promise((resolve, reject) => { | | 98 | + fs.open(filepath, 'r', common.mustSucceed((fd) => { | | 99 | + try { | | 100 | + assert.throws( | | 101 | + () => fs.read(fd, emptyBuffer, 0, 0, { not: 'a number' }, common.mustNotCall()), | | 102 | + { code: 'ERR_INVALID_ARG_TYPE' } | | 103 | + ); | | 104 | + assert.throws( | | 105 | + () => fs.read(fd, { buffer: emptyBuffer, offset: 0, length: 0, position: 'string' }, common.mustNotCall()), | | 106 | + { code: 'ERR_INVALID_ARG_TYPE' } | | 107 | + ); | | 108 | + resolve(); | | 109 | + } catch (err) { | | 110 | + reject(err); | | 111 | + } finally { | | 112 | + fs.close(fd, common.mustSucceed()); | | 113 | + } | | 114 | + })); | | 115 | + }); | | 116 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-read-promises-position-validation.mjs +25-1Lines changed: 25 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -79,6 +79,30 @@ async function testInvalid(code, position) { | 79 | 79 | for (const badTypeValue of [ | 80 | 80 | false, true, '1', Symbol(1), {}, [], () => {}, Promise.resolve(1), | 81 | 81 | ]) { | 82 | | - testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue); | | 82 | + await testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue); | | 83 | + } | | 84 | +} | | 85 | + | | 86 | +{ | | 87 | + const emptyBuffer = Buffer.alloc(0); | | 88 | + let fh; | | 89 | + try { | | 90 | + fh = await fs.promises.open(filepath, 'r'); | | 91 | + await assert.rejects( | | 92 | + fh.read(emptyBuffer, 0, 0, { not: 'a number' }), | | 93 | + { code: 'ERR_INVALID_ARG_TYPE' } | | 94 | + ); | | 95 | + } finally { | | 96 | + await fh?.close(); | | 97 | + } | | 98 | + | | 99 | + try { | | 100 | + fh = await fs.promises.open(filepath, 'r'); | | 101 | + await assert.rejects( | | 102 | + fh.read({ buffer: emptyBuffer, offset: 0, length: 0, position: 'string' }), | | 103 | + { code: 'ERR_INVALID_ARG_TYPE' } | | 104 | + ); | | 105 | + } finally { | | 106 | + await fh?.close(); | 83 | 107 | } | 84 | 108 | } |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-readSync-position-validation.mjs +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,3 +77,21 @@ function testInvalid(code, position) { | 77 | 77 | testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue); | 78 | 78 | } | 79 | 79 | } | | 80 | + | | 81 | +{ | | 82 | + const emptyBuffer = Buffer.alloc(0); | | 83 | + let fdSync; | | 84 | + try { | | 85 | + fdSync = fs.openSync(filepath, 'r'); | | 86 | + assert.throws( | | 87 | + () => fs.readSync(fdSync, emptyBuffer, 0, 0, { not: 'a number' }), | | 88 | + { code: 'ERR_INVALID_ARG_TYPE' } | | 89 | + ); | | 90 | + assert.throws( | | 91 | + () => fs.readSync(fdSync, emptyBuffer, { offset: 0, length: 0, position: 'string' }), | | 92 | + { code: 'ERR_INVALID_ARG_TYPE' } | | 93 | + ); | | 94 | + } finally { | | 95 | + if (fdSync) fs.closeSync(fdSync); | | 96 | + } | | 97 | +} |
|