Skip to content

Commit

Permalink
[FIX] len cannot exceed maximal Array length
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Mar 21, 2015
1 parent a4cb6ce commit a1a9a47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var isNumber = require( 'validate.io-number-primitive' );
* @returns {Array} linearly spaced numeric array
*/
function incrspace( x1, x2, inc ) {
var arr, len, i;
var arr, len, i, maxLength;
if ( !isNumber( x1 ) ) {
throw new TypeError( 'incrspace()::invalid input argument. Start must be numeric. Value: `' + x1 + '`.' );
}
Expand All @@ -60,6 +60,12 @@ function incrspace( x1, x2, inc ) {
}
// Calculate the array length:
len = Math.ceil( ( x2-x1 ) / inc );

maxLength = Math.pow(2,32) - 1;
if ( len >= maxLength ) {
throw new RangeError( 'incrspace()::invalid input arguments. (start - stop) / increment exceeds maximum Array length.' );
}

if ( len < 0 ) {
return [ x1 ];
}
Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ describe( 'compute-incrspace', function tests() {
}
});

it( 'should throw an error if the maximum Array length is exceeded', function test() {

var values = [
0.000000000000001,
0.00000000000000000000001,
0.000000000000000000000000001,
0
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( RangeError );
}

function badValue( value ) {
return function() {
incrspace( 0, 10, value );
};
}
});

it( 'should return a linearly spaced array', function test() {
var start, stop, expected, actual;

Expand Down

0 comments on commit a1a9a47

Please sign in to comment.