Permalink
Browse files

[attributedlabel] Safety checks before calling CFRelease.

Closes #521.
  • Loading branch information...
1 parent 08e1caf commit cbb7dd7acf5569aba3133d24ea1c7a5dbf69aa25 @jverkoey committed Mar 9, 2014
Showing with 37 additions and 23 deletions.
  1. +37 −23 src/attributedlabel/src/NIAttributedLabel.m
@@ -57,6 +57,10 @@ CGSize NISizeOfAttributedStringConstrainedToSize(NSAttributedString* attributedS
CFAttributedStringRef attributedStringRef = (__bridge CFAttributedStringRef)attributedString;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributedStringRef);
+ NIDASSERT(NULL != framesetter);
+ if (NULL == framesetter) {
+ return CGSizeZero;
+ }
CFRange range = CFRangeMake(0, 0);
// This logic adapted from @mattt's TTTAttributedLabel
@@ -85,10 +89,7 @@ CGSize NISizeOfAttributedStringConstrainedToSize(NSAttributedString* attributedS
CGSize newSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, range, NULL, constraintSize, NULL);
- if (nil != framesetter) {
- CFRelease(framesetter);
- framesetter = nil;
- }
+ CFRelease(framesetter);
return CGSizeMake(NICGFloatCeil(newSize.width), NICGFloatCeil(newSize.height));
}
@@ -169,8 +170,18 @@ - (CTFrameRef)textFrame {
NSMutableAttributedString* attributedStringWithLinks = [self mutableAttributedStringWithAdditions];
CFAttributedStringRef attributedString = (__bridge CFAttributedStringRef)attributedStringWithLinks;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributedString);
+ NIDASSERT(NULL != framesetter);
+ if (NULL == framesetter) {
+ return NULL;
+ }
CGMutablePathRef path = CGPathCreateMutable();
+ NIDASSERT(NULL != path);
+ if (NULL == path) {
+ CFRelease(framesetter);
+ return NULL;
+ }
+
CGPathAddRect(path, NULL, self.bounds);
CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
self.textFrame = textFrame;
@@ -1092,25 +1103,28 @@ - (NSMutableAttributedString *)mutableAttributedStringWithAdditions {
CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, (__bridge void *)labelImage);
- // Character to use as recommended by kCTRunDelegateAttributeName documentation.
- unichar objectReplacementChar = 0xFFFC;
- NSString *objectReplacementString = [NSString stringWithCharacters:&objectReplacementChar length:1];
- NSMutableAttributedString* space = [[NSMutableAttributedString alloc] initWithString:objectReplacementString];
-
- CFRange range = CFRangeMake(0, 1);
- CFMutableAttributedStringRef spaceString =
- (__bridge_retained CFMutableAttributedStringRef)space;
- CFAttributedStringSetAttribute(spaceString, range, kCTRunDelegateAttributeName, delegate);
- // Explicitly set the writing direction of this string to LTR, because in 'drawImages' we draw
- // for LTR by drawing at offset to offset + width vs to offset - width as you would for RTL.
- CFAttributedStringSetAttribute(spaceString,
- range,
- kCTWritingDirectionAttributeName,
- (__bridge CFArrayRef)@[@(kCTWritingDirectionLeftToRight)]);
- CFRelease(delegate);
- CFRelease(spaceString);
-
- [attributedString insertAttributedString:space atIndex:labelImage.index];
+ // If this asserts then we're not going to be able to attach the image to the label.
+ NIDASSERT(NULL != delegate);
+ if (NULL != delegate) {
+ // Character to use as recommended by kCTRunDelegateAttributeName documentation.
+ unichar objectReplacementChar = 0xFFFC;
+ NSString *objectReplacementString = [NSString stringWithCharacters:&objectReplacementChar length:1];
+ NSMutableAttributedString* space = [[NSMutableAttributedString alloc] initWithString:objectReplacementString];
+
+ CFRange range = CFRangeMake(0, 1);
+ CFMutableAttributedStringRef spaceString = (__bridge_retained CFMutableAttributedStringRef)space;
+ CFAttributedStringSetAttribute(spaceString, range, kCTRunDelegateAttributeName, delegate);
+ // Explicitly set the writing direction of this string to LTR, because in 'drawImages' we draw
+ // for LTR by drawing at offset to offset + width vs to offset - width as you would for RTL.
+ CFAttributedStringSetAttribute(spaceString,
+ range,
+ kCTWritingDirectionAttributeName,
+ (__bridge CFArrayRef)@[@(kCTWritingDirectionLeftToRight)]);
+ CFRelease(delegate);
+ CFRelease(spaceString);
+
+ [attributedString insertAttributedString:space atIndex:labelImage.index];
+ }
}
}

0 comments on commit cbb7dd7

Please sign in to comment.