From e1cfc1d3c75f156bcadd8ff79f9c550480fb745a Mon Sep 17 00:00:00 2001 From: ibireme Date: Tue, 13 Sep 2016 14:59:09 +0800 Subject: [PATCH] fix potential crash when get attribute from NSAttributedString with invalid index. --- YYText/Utility/NSAttributedString+YYText.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/YYText/Utility/NSAttributedString+YYText.m b/YYText/Utility/NSAttributedString+YYText.m index 7d014dc9..63f95799 100644 --- a/YYText/Utility/NSAttributedString+YYText.m +++ b/YYText/Utility/NSAttributedString+YYText.m @@ -78,13 +78,14 @@ + (instancetype)yy_unarchiveFromData:(NSData *)data { } - (NSDictionary *)yy_attributesAtIndex:(NSUInteger)index { + if (index > self.length || self.length == 0) return nil; if (self.length > 0 && index == self.length) index--; return [self attributesAtIndex:index effectiveRange:NULL]; } - (id)yy_attribute:(NSString *)attributeName atIndex:(NSUInteger)index { if (!attributeName) return nil; - if (self.length == 0) return nil; + if (index > self.length || self.length == 0) return nil; if (self.length > 0 && index == self.length) index--; return [self attribute:attributeName atIndex:index effectiveRange:NULL]; }