浏览代码

fix potential crash when get attribute from NSAttributedString with invalid index.

ibireme 9 年之前
父节点
当前提交
e1cfc1d3c7
共有 1 个文件被更改,包括 2 次插入1 次删除
  1. 2 1
      YYText/Utility/NSAttributedString+YYText.m

+ 2 - 1
YYText/Utility/NSAttributedString+YYText.m

@@ -78,13 +78,14 @@ static double _YYDeviceSystemVersion() {
 }
 }
 
 
 - (NSDictionary *)yy_attributesAtIndex:(NSUInteger)index {
 - (NSDictionary *)yy_attributesAtIndex:(NSUInteger)index {
+    if (index > self.length || self.length == 0) return nil;
     if (self.length > 0 && index == self.length) index--;
     if (self.length > 0 && index == self.length) index--;
     return [self attributesAtIndex:index effectiveRange:NULL];
     return [self attributesAtIndex:index effectiveRange:NULL];
 }
 }
 
 
 - (id)yy_attribute:(NSString *)attributeName atIndex:(NSUInteger)index {
 - (id)yy_attribute:(NSString *)attributeName atIndex:(NSUInteger)index {
     if (!attributeName) return nil;
     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--;
     if (self.length > 0 && index == self.length) index--;
     return [self attribute:attributeName atIndex:index effectiveRange:NULL];
     return [self attribute:attributeName atIndex:index effectiveRange:NULL];
 }
 }