From 821961fc263e1b754fa58c8549aeb43f5f4821ed Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Tue, 15 Jan 2013 16:08:17 +0000 Subject: [PATCH] Argument check CPIndexSet indexSetWithIndex:. Without this check, an index set could be initialised with NaN which would later lead to an infinite loop when enumerating the index set. This error makes the error early and explicit. --- Foundation/CPIndexSet.j | 5 +++++ Tests/Foundation/CPIndexSetTest.j | 1 + 2 files changed, 6 insertions(+) diff --git a/Foundation/CPIndexSet.j b/Foundation/CPIndexSet.j index 33ecfbdf7..ffa8300f2 100644 --- a/Foundation/CPIndexSet.j +++ b/Foundation/CPIndexSet.j @@ -21,6 +21,7 @@ */ #include "CPRange.h" +#include "Foundation.h" @import "CPArray.j" @import "CPObject.j" @@ -79,6 +80,10 @@ */ - (id)initWithIndex:(CPInteger)anIndex { + if (!_IS_NUMERIC(anIndex)) + [CPException raise:CPInvalidArgumentException + reason:"Invalid index"]; + return [self initWithIndexesInRange:_CPMakeRange(anIndex, 1)]; } diff --git a/Tests/Foundation/CPIndexSetTest.j b/Tests/Foundation/CPIndexSetTest.j index fefbd220d..adc0a0f43 100644 --- a/Tests/Foundation/CPIndexSetTest.j +++ b/Tests/Foundation/CPIndexSetTest.j @@ -202,6 +202,7 @@ function descriptionWithoutEntity(aString) [self assertTrue:[[CPIndexSet indexSetWithIndex:1] containsIndex:1]]; [self assertTrue:[[CPIndexSet indexSetWithIndex:0] containsIndex:0]]; [self assertFalse:[[CPIndexSet indexSetWithIndex:0] containsIndex:1]]; + [self assertThrows:function() { [CPIndexSet indexSetWithIndex:NaN] }]; } - (void)testIndexSetWithIndexesInRange