@ -123,6 +123,15 @@ SlowBuffer.prototype.slice = function(start, end) {
} ;
} ;
function coerce ( length ) {
// Coerce length to a number (possibly NaN), round up
// in case it's fractional (e.g. 123.456) then do a
// double negate to coerce a NaN to 0. Easy, right?
length = ~ ~ Math . ceil ( + length ) ;
return length < 0 ? 0 : length ;
}
// Buffer
// Buffer
function Buffer ( subject , encoding , offset ) {
function Buffer ( subject , encoding , offset ) {
@ -134,14 +143,14 @@ function Buffer(subject, encoding, offset) {
// Are we slicing?
// Are we slicing?
if ( typeof offset === 'number' ) {
if ( typeof offset === 'number' ) {
this . length = encoding ;
this . length = coerce ( encoding ) ;
this . parent = subject ;
this . parent = subject ;
this . offset = offset ;
this . offset = offset ;
} else {
} else {
// Find the length
// Find the length
switch ( type = typeof subject ) {
switch ( type = typeof subject ) {
case 'number' :
case 'number' :
this . length = subject ;
this . length = coerce ( subject ) ;
break ;
break ;
case 'string' :
case 'string' :
@ -149,7 +158,7 @@ function Buffer(subject, encoding, offset) {
break ;
break ;
case 'object' : // Assume object is an array
case 'object' : // Assume object is an array
this . length = subject . length ;
this . length = coerce ( subject . length ) ;
break ;
break ;
default :
default :