Skip to content

Commit f77fa92

Browse files
committed
fix max size check in Buffer constructor
Copied from nodejs/node#657
1 parent 5a951d1 commit f77fa92

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ function Buffer (subject, encoding, noZero) {
7373
// Find the length
7474
var length
7575
if (type === 'number')
76-
length = subject > 0 ? subject >>> 0 : 0
76+
length = +subject
7777
else if (type === 'string') {
7878
length = Buffer.byteLength(subject, encoding)
7979
} else if (type === 'object' && subject !== null) { // assume object is array-like
8080
if (subject.type === 'Buffer' && isArray(subject.data))
8181
subject = subject.data
82-
length = +subject.length > 0 ? Math.floor(+subject.length) : 0
82+
length = +subject.length
8383
} else {
8484
throw new TypeError('must start with number, buffer, array or string')
8585
}
@@ -88,6 +88,11 @@ function Buffer (subject, encoding, noZero) {
8888
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
8989
'size: 0x' + kMaxLength.toString(16) + ' bytes')
9090

91+
if (length < 0)
92+
length = 0
93+
else
94+
length >>>= 0 // Coerce to uint32.
95+
9196
var self = this
9297
if (Buffer.TYPED_ARRAY_SUPPORT) {
9398
// Preferred: Return an augmented `Uint8Array` instance for best performance

0 commit comments

Comments
 (0)