Skip to content
Browse files

Fixed some memory leaks and potential crashes.

  • Loading branch information...
1 parent a94ade8 commit 40c2ed04fa303d97ea85d59f8d2ecf8284302471 @beelsebob committed Aug 26, 2012
View
5 CoreParse/Grammar/CPGrammar.m
@@ -310,7 +310,10 @@ - (id)initWithStart:(NSString *)initStart backusNaurForm:(NSString *)bnf error:(
if ([del err] != nil)
{
- *error = [[[del err] copy] autorelease];
+ if (NULL != error)
+ {
+ *error = [[[del err] copy] autorelease];
+ }
return nil;
}
View
7 CoreParse/Grammar/CPGrammarInternal.m
@@ -246,7 +246,10 @@ - (NSArray *)tidyRightHandSides:(NSArray *)oldRules error:(NSError **)error
NSError *invalidRuleError = [self checkRulesForErrors:oldRules];
if (nil != invalidRuleError)
{
- *error = invalidRuleError;
+ if (NULL != error)
+ {
+ *error = invalidRuleError;
+ }
return nil;
}
@@ -340,7 +343,7 @@ - (NSDictionary *)nameNewRules:(NSSet *)rhsElements withRules:(NSArray *)oldRule
- (NSArray *)addRHSRules:(NSDictionary *)newRules toRules:(NSArray *)oldRules
{
- NSMutableArray *rules = [[NSMutableArray alloc] initWithArray:oldRules];
+ NSMutableArray *rules = [[[NSMutableArray alloc] initWithArray:oldRules] autorelease];
Class rhsItemClass = [CPRHSItemResult class];
for (CPRHSItem *item in newRules)
View
39 CoreParse/Grammar/CPRHSItem.m
@@ -122,11 +122,14 @@ - (NSSet *)tagNamesWithError:(NSError **)err
[duplicateTags intersectSet:newTagNames];
if ([duplicateTags count] > 0)
{
- *err = [NSError errorWithDomain:CPEBNFParserErrorDomain
- code:CPErrorCodeDuplicateTag
- userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
- [NSString stringWithFormat:@"Duplicate tag names %@ in same part of alternative is not allowed in \"%@\".", duplicateTags, self], NSLocalizedDescriptionKey,
- nil]];
+ if (NULL != err)
+ {
+ *err = [NSError errorWithDomain:CPEBNFParserErrorDomain
+ code:CPErrorCodeDuplicateTag
+ userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
+ [NSString stringWithFormat:@"Duplicate tag names %@ in same part of alternative is not allowed in \"%@\".", duplicateTags, self], NSLocalizedDescriptionKey,
+ nil]];
+ }
return nil;
}
[tagNamesInAlternative unionSet:newTagNames];
@@ -135,11 +138,14 @@ - (NSSet *)tagNamesWithError:(NSError **)err
{
if ([tagNamesInAlternative containsObject:tagName])
{
- *err = [NSError errorWithDomain:CPEBNFParserErrorDomain
- code:CPErrorCodeDuplicateTag
- userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
- [NSString stringWithFormat:@"Duplicate tag names (%@) in same part of alternative is not allowed in \"%@\".", tagName, self], NSLocalizedDescriptionKey,
- nil]];
+ if (NULL != err)
+ {
+ *err = [NSError errorWithDomain:CPEBNFParserErrorDomain
+ code:CPErrorCodeDuplicateTag
+ userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
+ [NSString stringWithFormat:@"Duplicate tag names (%@) in same part of alternative is not allowed in \"%@\".", tagName, self], NSLocalizedDescriptionKey,
+ nil]];
+ }
return nil;
}
[tagNamesInAlternative addObject:tagName];
@@ -151,11 +157,14 @@ - (NSSet *)tagNamesWithError:(NSError **)err
if ([tagNames count] > 0 && [self repeats])
{
- *err = [NSError errorWithDomain:CPEBNFParserErrorDomain
- code:CPErrorCodeDuplicateTag
- userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
- [NSString stringWithFormat:@"Tag names are not allowed within repeating section of rule \"%@\".", self], NSLocalizedDescriptionKey,
- nil]];
+ if (NULL != err)
+ {
+ *err = [NSError errorWithDomain:CPEBNFParserErrorDomain
+ code:CPErrorCodeDuplicateTag
+ userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
+ [NSString stringWithFormat:@"Tag names are not allowed within repeating section of rule \"%@\".", self], NSLocalizedDescriptionKey,
+ nil]];
+ }
return nil;
}
View
1 CoreParse/Syntax Tree/CPSyntaxTree.m
@@ -60,6 +60,7 @@ - (void)dealloc
{
[rule release];
[children release];
+ [tagValues release];
[super dealloc];
}
View
60 CoreParse/Tokenisation/Token Recognisers/CPQuotedRecogniser.m
@@ -106,64 +106,78 @@ - (void)dealloc
- (CPToken *)recogniseTokenInString:(NSString *)tokenString currentTokenPosition:(NSUInteger *)tokenPosition
{
- NSString *sq = [self startQuote];
- NSString *eq = [self endQuote];
- NSString *es = [self escapeSequence];
NSString *(^er)(NSString *tokenStream, NSUInteger *quotePosition) = [self escapeReplacer];
- NSUInteger startQuoteLength = [sq length];
- NSUInteger endQuoteLength = [eq length];
+ NSUInteger startQuoteLength = [startQuote length];
+ NSUInteger endQuoteLength = [endQuote length];
- NSUInteger inputLength = [tokenString length];
- NSRange searchRange = NSMakeRange(*tokenPosition, MIN(inputLength - *tokenPosition,startQuoteLength + endQuoteLength + maximumLength));
- NSRange range = [tokenString rangeOfString:sq options:NSLiteralSearch | NSAnchoredSearch range:searchRange];
+ long inputLength = [tokenString length];
+ CFRange searchRange = CFRangeMake(*tokenPosition, MIN(inputLength - *tokenPosition,startQuoteLength + endQuoteLength + maximumLength));
+ CFRange range;
+ BOOL matched = CFStringFindWithOptions((CFStringRef)tokenString, (CFStringRef)startQuote, searchRange, kCFCompareAnchored, &range);
- NSMutableString *outputString = [NSMutableString string];
+ CFMutableStringRef outputString = CFStringCreateMutable(kCFAllocatorDefault, 0);
- if (NSNotFound != range.location)
+ if (matched)
{
searchRange.location = searchRange.location + range.length;
searchRange.length = searchRange.length - range.length;
- NSRange endRange = [tokenString rangeOfString:eq options:NSLiteralSearch range:searchRange];
- NSRange escapeRange = nil == es ? NSMakeRange(NSNotFound, 0) : [tokenString rangeOfString:es options:NSLiteralSearch range:searchRange];
+ CFRange endRange;
+ CFRange escapeRange;
+ BOOL matchedEndSequence = CFStringFindWithOptions((CFStringRef)tokenString, (CFStringRef)endQuote, searchRange, 0L, &endRange);
+ BOOL matchedEscapeSequence = nil == escapeSequence ? NO : CFStringFindWithOptions((CFStringRef)tokenString, (CFStringRef)escapeSequence, searchRange, 0L, &escapeRange);
- while (NSNotFound != endRange.location && searchRange.location < inputLength)
+ while (matchedEndSequence && searchRange.location < inputLength)
{
- if (endRange.location < escapeRange.location)
+ if (!matchedEscapeSequence || endRange.location < escapeRange.location)
{
*tokenPosition = endRange.location + endRange.length;
- [outputString appendString:[tokenString substringWithRange:NSMakeRange(searchRange.location, endRange.location - searchRange.location)]];
- return [CPQuotedToken content:outputString quotedWith:sq name:[self name]];
+ CFStringRef substr = CFStringCreateWithSubstring(kCFAllocatorDefault, (CFStringRef)tokenString, CFRangeMake(searchRange.location, endRange.location - searchRange.location));
+ CFStringAppend(outputString, substr);
+ CFRelease(substr);
+ CPQuotedToken *t = [CPQuotedToken content:(NSString *)outputString quotedWith:startQuote name:[self name]];
+ CFRelease(outputString);
+ return t;
}
else
{
NSUInteger quotedPosition = escapeRange.location + escapeRange.length;
- NSString *escapedStuff = nil;
+ CFStringRef substr = CFStringCreateWithSubstring(kCFAllocatorDefault, (CFStringRef)tokenString, CFRangeMake(searchRange.location, escapeRange.location - searchRange.location));
+ CFStringAppend(outputString, substr);
+ CFRelease(substr);
+ BOOL appended = NO;
if (nil != er)
{
- escapedStuff = er(tokenString, &quotedPosition);
+ NSString *s = er(tokenString, &quotedPosition);
+ if (nil != s)
+ {
+ appended = YES;
+ CFStringAppend(outputString, (CFStringRef)s);
+ }
}
- if (nil == escapedStuff)
+ if (!appended)
{
- escapedStuff = [tokenString substringWithRange:NSMakeRange(escapeRange.location + escapeRange.length, 1)];
+ substr = CFStringCreateWithSubstring(kCFAllocatorDefault, (CFStringRef)tokenString, CFRangeMake(escapeRange.location + escapeRange.length, 1));
+ CFStringAppend(outputString, substr);
+ CFRelease(substr);
quotedPosition += 1;
}
- [outputString appendFormat:@"%@%@", [tokenString substringWithRange:NSMakeRange(searchRange.location, escapeRange.location - searchRange.location)], escapedStuff];
searchRange.length = searchRange.location + searchRange.length - quotedPosition;
searchRange.location = quotedPosition;
if (endRange.location < searchRange.location)
{
- endRange = [tokenString rangeOfString:eq options:NSLiteralSearch range:searchRange];
+ matchedEndSequence = CFStringFindWithOptions((CFStringRef)tokenString, (CFStringRef)endQuote, searchRange, 0L, &endRange);
}
if (escapeRange.location < searchRange.location)
{
- escapeRange = [tokenString rangeOfString:es options:NSLiteralSearch range:searchRange];
+ matchedEscapeSequence = CFStringFindWithOptions((CFStringRef)tokenString, (CFStringRef)escapeSequence, searchRange, 0L, &escapeRange);
}
}
}
}
+ CFRelease(outputString);
return nil;
}
View
7 CoreParseTests/CoreParseTests.m
@@ -626,9 +626,10 @@ - (void)testParseResultParsing
- (void)runMapCSSTokeniser:(CPTokenStream *)result
{
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- [mapCssTokeniser tokenise:mapCssInput into:result];
- [pool drain];
+ @autoreleasepool
+ {
+ [mapCssTokeniser tokenise:mapCssInput into:result];
+ }
}
- (void)testJSONParsing

0 comments on commit 40c2ed0

Please sign in to comment.
Something went wrong with that request. Please try again.