|
8 | 8 | SlowBuffer, |
9 | 9 | kMaxLength, |
10 | 10 | } = require('buffer'); |
| 11 | +const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH; |
11 | 12 |
|
12 | 13 | // Verify the maximum Uint8Array size. There is no concrete limit by spec. The |
13 | 14 | // internal limits should be updated if this fails. |
@@ -107,6 +108,13 @@ const outOfRangeError = { |
107 | 108 | name: 'RangeError' |
108 | 109 | }; |
109 | 110 |
|
| 111 | +const stringTooLongError = { |
| 112 | + message: `Cannot create a string longer than 0x${kStringMaxLength.toString(16)}` + |
| 113 | + ' characters', |
| 114 | + code: 'ERR_STRING_TOO_LONG', |
| 115 | + name: 'Error', |
| 116 | +}; |
| 117 | + |
110 | 118 | // Try to write a 0-length string beyond the end of b |
111 | 119 | assert.throws(() => b.write('', 2048), outOfRangeError); |
112 | 120 |
|
@@ -1147,6 +1155,22 @@ assert.throws(() => { |
1147 | 1155 | assert.strictEqual(ubuf.buffer.byteLength, 10); |
1148 | 1156 | } |
1149 | 1157 |
|
| 1158 | +// Invalid length of Buffer.utf8Write |
| 1159 | +{ |
| 1160 | + const ubuf = Buffer.allocUnsafeSlow(2 ** 32); |
| 1161 | + assert.throws(() => { |
| 1162 | + ubuf.utf8Write('a', 2, kStringMaxLength + 2); |
| 1163 | + }, stringTooLongError); |
| 1164 | +} |
| 1165 | + |
| 1166 | +// Invalid length of Buffer.write |
| 1167 | +{ |
| 1168 | + const ubuf = Buffer.allocUnsafeSlow(2 ** 32); |
| 1169 | + assert.throws(() => { |
| 1170 | + ubuf.write('a', 2, kStringMaxLength + 2); |
| 1171 | + }, stringTooLongError); |
| 1172 | +} |
| 1173 | + |
1150 | 1174 | // Regression test to verify that an empty ArrayBuffer does not throw. |
1151 | 1175 | Buffer.from(new ArrayBuffer()); |
1152 | 1176 |
|
|
0 commit comments