| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,6 +51,7 @@ const { | |||
| 51 | 51 | TypedArrayPrototypeGetLength, | |
| 52 | 52 | TypedArrayPrototypeSet, | |
| 53 | 53 | TypedArrayPrototypeSlice, | |
| 54 | + TypedArrayPrototypeSubarray, | ||
| 54 | 55 | Uint8Array, | |
| 55 | 56 | } = primordials; | |
| 56 | 57 | ||
@@ -614,25 +615,55 @@ Buffer.concat = function concat(list, length) { | |||
| 614 | 615 | if (length === undefined) { | |
| 615 | 616 | length = 0; | |
| 616 | 617 | for (let i = 0; i < list.length; i++) { | |
| 617 | - if (list[i].length) { | ||
| 618 | - length += list[i].length; | ||
| 618 | + const buf = list[i]; | ||
| 619 | + if (!isUint8Array(buf)) { | ||
| 620 | + // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE. | ||
| 621 | + // Instead, find the proper error code for this. | ||
| 622 | + throw new ERR_INVALID_ARG_TYPE( | ||
| 623 | + `list[${i}]`, ['Buffer', 'Uint8Array'], buf); | ||
| 619 | 624 | } | |
| 625 | + length += TypedArrayPrototypeGetByteLength(buf); | ||
| 620 | 626 | } | |
| 621 | - } else { | ||
| 622 | - validateOffset(length, 'length'); | ||
| 627 | + | ||
| 628 | + const buffer = allocate(length); | ||
| 629 | + let pos = 0; | ||
| 630 | + for (let i = 0; i < list.length; i++) { | ||
| 631 | + const buf = list[i]; | ||
| 632 | + const bufLength = TypedArrayPrototypeGetByteLength(buf); | ||
| 633 | + TypedArrayPrototypeSet(buffer, buf, pos); | ||
| 634 | + pos += bufLength; | ||
| 635 | + } | ||
| 636 | + | ||
| 637 | + if (pos < length) { | ||
| 638 | + TypedArrayPrototypeFill(buffer, 0, pos, length); | ||
| 639 | + } | ||
| 640 | + return buffer; | ||
| 623 | 641 | } | |
| 624 | 642 | ||
| 625 | - const buffer = Buffer.allocUnsafe(length); | ||
| 626 | - let pos = 0; | ||
| 643 | + validateOffset(length, 'length'); | ||
| 627 | 644 | for (let i = 0; i < list.length; i++) { | |
| 628 | - const buf = list[i]; | ||
| 629 | - if (!isUint8Array(buf)) { | ||
| 645 | + if (!isUint8Array(list[i])) { | ||
| 630 | 646 | // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE. | |
| 631 | 647 | // Instead, find the proper error code for this. | |
| 632 | 648 | throw new ERR_INVALID_ARG_TYPE( | |
| 633 | 649 | `list[${i}]`, ['Buffer', 'Uint8Array'], list[i]); | |
| 634 | 650 | } | |
| 635 | - pos += _copyActual(buf, buffer, pos, 0, buf.length, true); | ||
| 651 | + } | ||
| 652 | + | ||
| 653 | + const buffer = allocate(length); | ||
| 654 | + let pos = 0; | ||
| 655 | + for (let i = 0; i < list.length; i++) { | ||
| 656 | + const buf = list[i]; | ||
| 657 | + const bufLength = TypedArrayPrototypeGetByteLength(buf); | ||
| 658 | + if (pos + bufLength > length) { | ||
| 659 | + TypedArrayPrototypeSet(buffer, | ||
| 660 | + TypedArrayPrototypeSubarray(buf, 0, length - pos), | ||
| 661 | + pos); | ||
| 662 | + pos = length; | ||
| 663 | + break; | ||
| 664 | + } | ||
| 665 | + TypedArrayPrototypeSet(buffer, buf, pos); | ||
| 666 | + pos += bufLength; | ||
| 636 | 667 | } | |
| 637 | 668 | ||
| 638 | 669 | // Note: `length` is always equal to `buffer.length` at this point | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -123,3 +123,13 @@ assert.deepStrictEqual( | |||
| 123 | 123 | assert.deepStrictEqual(Buffer.concat([new Uint8Array([0x41, 0x42]), | |
| 124 | 124 | new Uint8Array([0x43, 0x44])]), | |
| 125 | 125 | Buffer.from('ABCD')); | |
| 126 | + | ||
| 127 | + // Spoofed length getter should not cause uninitialized memory exposure | ||
| 128 | + { | ||
| 129 | + const u8_1 = new Uint8Array([1, 2, 3, 4]); | ||
| 130 | + const u8_2 = new Uint8Array([5, 6, 7, 8]); | ||
| 131 | + Object.defineProperty(u8_1, 'length', { get() { return 100; } }); | ||
| 132 | + const buf = Buffer.concat([u8_1, u8_2]); | ||
| 133 | + assert.strictEqual(buf.length, 8); | ||
| 134 | + assert.deepStrictEqual(buf, Buffer.from([1, 2, 3, 4, 5, 6, 7, 8])); | ||
| 135 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments