-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
buffer: make
buflen
in integer range
- Loading branch information
Showing
2 changed files
with
73 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
// Buffer with size > INT32_MAX | ||
common.skipIf32Bits(); | ||
|
||
// Test Buffer size larger than integer range | ||
const assert = require('assert'); | ||
const { | ||
SlowBuffer, | ||
} = require('buffer'); | ||
const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH; | ||
|
||
const stringTooLongError = { | ||
message: `Cannot create a string longer than 0x${kStringMaxLength.toString(16)}` + | ||
' characters', | ||
code: 'ERR_STRING_TOO_LONG', | ||
name: 'Error', | ||
}; | ||
|
||
const size = 2 ** 31; | ||
|
||
// Test Buffer.toString | ||
{ | ||
try { | ||
assert.throws(() => SlowBuffer(size).toString('utf8'), stringTooLongError); | ||
assert.throws(() => Buffer.alloc(size).toString('utf8'), stringTooLongError); | ||
assert.throws(() => Buffer.allocUnsafe(size).toString('utf8'), stringTooLongError); | ||
assert.throws(() => Buffer.allocUnsafeSlow(size).toString('utf8'), stringTooLongError); | ||
} catch (e) { | ||
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') { | ||
throw e; | ||
} | ||
common.skip('insufficient space for Buffer.alloc'); | ||
} | ||
} | ||
|
||
// Test Buffer.write | ||
{ | ||
try { | ||
const buf = Buffer.alloc(size); | ||
assert.strictEqual(buf.write('a', 2, kStringMaxLength), 1); | ||
assert.strictEqual(buf.write('a', 2, size), 1); | ||
} catch (e) { | ||
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') { | ||
throw e; | ||
} | ||
common.skip('insufficient space for Buffer.alloc'); | ||
} | ||
} |