Skip to content

Commit

Permalink
fix max size check in Buffer constructor
Browse files Browse the repository at this point in the history
Copied from nodejs/node#657
  • Loading branch information
feross committed Feb 10, 2015
1 parent 5a951d1 commit f77fa92
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ function Buffer (subject, encoding, noZero) {
// Find the length
var length
if (type === 'number')
length = subject > 0 ? subject >>> 0 : 0
length = +subject
else if (type === 'string') {
length = Buffer.byteLength(subject, encoding)
} else if (type === 'object' && subject !== null) { // assume object is array-like
if (subject.type === 'Buffer' && isArray(subject.data))
subject = subject.data
length = +subject.length > 0 ? Math.floor(+subject.length) : 0
length = +subject.length
} else {
throw new TypeError('must start with number, buffer, array or string')
}
Expand All @@ -88,6 +88,11 @@ function Buffer (subject, encoding, noZero) {
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
'size: 0x' + kMaxLength.toString(16) + ' bytes')

if (length < 0)
length = 0
else
length >>>= 0 // Coerce to uint32.

var self = this
if (Buffer.TYPED_ARRAY_SUPPORT) {
// Preferred: Return an augmented `Uint8Array` instance for best performance
Expand Down

0 comments on commit f77fa92

Please sign in to comment.