GeezerVison is showing github.com — lib,test: do not hardcode Buffer.kMaxLength · nodejs/node@5bca8fe
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/internal/blob.js +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,9 @@ const { | 24 | 24 | concat, | 25 | 25 | getDataObject, | 26 | 26 | } = internalBinding('blob'); | | 27 | +const { | | 28 | + kMaxLength, | | 29 | +} = internalBinding('buffer'); | 27 | 30 |
| 28 | 31 | const { | 29 | 32 | TextDecoder, | @@ -62,7 +65,6 @@ const { | 62 | 65 | } = require('internal/errors'); | 63 | 66 |
| 64 | 67 | const { | 65 | | - isUint32, | 66 | 68 | validateDictionary, | 67 | 69 | } = require('internal/validators'); | 68 | 70 |
| @@ -158,8 +160,8 @@ class Blob { | 158 | 160 | return src; | 159 | 161 | }); | 160 | 162 |
| 161 | | - if (!isUint32(length)) | 162 | | - throw new ERR_BUFFER_TOO_LARGE(0xFFFFFFFF); | | 163 | + if (length > kMaxLength) | | 164 | + throw new ERR_BUFFER_TOO_LARGE(kMaxLength); | 163 | 165 |
| 164 | 166 | this[kHandle] = _createBlob(sources_, length); | 165 | 167 | this[kLength] = length; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-blob-buffer-too-large.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | const common = require('../common'); | 5 | 5 | const assert = require('assert'); | 6 | | -const { Blob } = require('buffer'); | | 6 | +const { Blob, kMaxLength } = require('buffer'); | 7 | 7 |
| 8 | 8 | if (common.isFreeBSD) | 9 | 9 | common.skip('Oversized buffer make the FreeBSD CI runner crash'); | 10 | 10 |
| 11 | 11 | try { | 12 | | - new Blob([new Uint8Array(0xffffffff), [1]]); | | 12 | + new Blob([new Uint8Array(kMaxLength), [1]]); | 13 | 13 | } catch (e) { | 14 | 14 | if ( | 15 | 15 | e.message === 'Array buffer allocation failed' || | 16 | | - e.message === 'Invalid typed array length: 4294967295' | | 16 | + e.message === `Invalid typed array length: ${kMaxLength}` | 17 | 17 | ) { | 18 | 18 | common.skip( | 19 | 19 | 'Insufficient memory on this platform for oversized buffer test.' | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-buffer-alloc.js +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,13 +4,16 @@ const common = require('../common'); | 4 | 4 | const assert = require('assert'); | 5 | 5 | const vm = require('vm'); | 6 | 6 |
| 7 | | -const SlowBuffer = require('buffer').SlowBuffer; | | 7 | +const { | | 8 | + SlowBuffer, | | 9 | + kMaxLength, | | 10 | +} = require('buffer'); | 8 | 11 |
| 9 | 12 | // Verify the maximum Uint8Array size. There is no concrete limit by spec. The | 10 | 13 | // internal limits should be updated if this fails. | 11 | 14 | assert.throws( | 12 | | - () => new Uint8Array(2 ** 32 + 1), | 13 | | - { message: 'Invalid typed array length: 4294967297' } | | 15 | + () => new Uint8Array(kMaxLength + 1), | | 16 | + { message: `Invalid typed array length: ${kMaxLength + 1}` }, | 14 | 17 | ); | 15 | 18 |
| 16 | 19 | const b = Buffer.allocUnsafe(1024); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-buffer-over-max-length.js -10Lines changed: 0 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,18 +12,8 @@ const bufferMaxSizeMsg = { | 12 | 12 | name: 'RangeError', | 13 | 13 | }; | 14 | 14 |
| 15 | | -assert.throws(() => Buffer((-1 >>> 0) + 2), bufferMaxSizeMsg); | 16 | | -assert.throws(() => SlowBuffer((-1 >>> 0) + 2), bufferMaxSizeMsg); | 17 | | -assert.throws(() => Buffer.alloc((-1 >>> 0) + 2), bufferMaxSizeMsg); | 18 | | -assert.throws(() => Buffer.allocUnsafe((-1 >>> 0) + 2), bufferMaxSizeMsg); | 19 | | -assert.throws(() => Buffer.allocUnsafeSlow((-1 >>> 0) + 2), bufferMaxSizeMsg); | 20 | | - | 21 | 15 | assert.throws(() => Buffer(kMaxLength + 1), bufferMaxSizeMsg); | 22 | 16 | assert.throws(() => SlowBuffer(kMaxLength + 1), bufferMaxSizeMsg); | 23 | 17 | assert.throws(() => Buffer.alloc(kMaxLength + 1), bufferMaxSizeMsg); | 24 | 18 | assert.throws(() => Buffer.allocUnsafe(kMaxLength + 1), bufferMaxSizeMsg); | 25 | 19 | assert.throws(() => Buffer.allocUnsafeSlow(kMaxLength + 1), bufferMaxSizeMsg); | 26 | | - | 27 | | -// issue GH-4331 | 28 | | -assert.throws(() => Buffer.allocUnsafe(0x100000001), bufferMaxSizeMsg); | 29 | | -assert.throws(() => Buffer.allocUnsafe(0xFFFFFFFFF), bufferMaxSizeMsg); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-buffer-tostring-rangeerror.js +11-6Lines changed: 11 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 | require('../common'); | 3 | 3 |
| 4 | | -// This test ensures that Node.js throws a RangeError when trying to convert a | 5 | | -// gigantic buffer into a string. | | 4 | +// This test ensures that Node.js throws an Error when trying to convert a | | 5 | +// large buffer into a string. | 6 | 6 | // Regression test for https://github.com/nodejs/node/issues/649. | 7 | 7 |
| 8 | 8 | const assert = require('assert'); | 9 | | -const SlowBuffer = require('buffer').SlowBuffer; | | 9 | +const { | | 10 | + SlowBuffer, | | 11 | + constants: { | | 12 | + MAX_STRING_LENGTH, | | 13 | + }, | | 14 | +} = require('buffer'); | 10 | 15 |
| 11 | | -const len = 1422561062959; | | 16 | +const len = MAX_STRING_LENGTH + 1; | 12 | 17 | const message = { | 13 | | - code: 'ERR_OUT_OF_RANGE', | 14 | | - name: 'RangeError', | | 18 | + code: 'ERR_STRING_TOO_LONG', | | 19 | + name: 'Error', | 15 | 20 | }; | 16 | 21 | assert.throws(() => Buffer(len).toString('utf8'), message); | 17 | 22 | assert.throws(() => SlowBuffer(len).toString('utf8'), message); | |
|