From c77a282aaf4f21cd8e9a6663ba149aab4b8d17dc Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Sat, 27 Aug 2011 23:50:18 +0100 Subject: [PATCH] Raise CPRangeException if CPString substringToIndex: is out of bounds. --- Foundation/CPString.j | 3 +++ Tests/Foundation/CPStringTest.j | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/Foundation/CPString.j b/Foundation/CPString.j index 3222c0b88..6aa13e9a0 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -314,6 +314,9 @@ var CPStringRegexSpecialCharacters = [ */ - (CPString)substringToIndex:(unsigned)anIndex { + if (anIndex > length) + [CPException raise:CPRangeException reason:"index out of bounds"]; + return substring(0, anIndex); } diff --git a/Tests/Foundation/CPStringTest.j b/Tests/Foundation/CPStringTest.j index 1389d493b..678a560ce 100644 --- a/Tests/Foundation/CPStringTest.j +++ b/Tests/Foundation/CPStringTest.j @@ -128,6 +128,18 @@ [self assert:"abcd" equals:["abcd" substringToIndex:4]]; [self assert:"abc" equals:["abcd" substringToIndex:3]]; [self assert:"" equals:["abcd" substringToIndex:0]]; + + var sawException = false; + try + { + [@"abcd" substringToIndex:5]; + } + catch (anException) + { + sawException = true; + [self assert:CPRangeException equals:[anException name]]; + } + [self assertTrue:sawException message:"expected CPRangeException"]; } - (void)testBoolValue