@@ -160,7 +160,7 @@ + (void)qmuisafety_NSString {
160
160
161
161
// index 越界
162
162
{
163
- BOOL isValidddatedIndex = index < selfObject.length ;
163
+ BOOL isValidddatedIndex = index <= selfObject.length ;
164
164
if (!isValidddatedIndex) {
165
165
NSString *logString = [NSString stringWithFormat: @" %@ 传入了一个超过字符串长度的 index: %@ ,原字符串为: %@ (%@ )" , NSStringFromSelector (originCMD), @(index), selfObject, @(selfObject.length)];
166
166
NSAssert (NO , logString);
@@ -171,13 +171,15 @@ + (void)qmuisafety_NSString {
171
171
172
172
// 保护从 emoji 等 ComposedCharacterSequence 中间裁剪的场景
173
173
{
174
- NSRange range = [selfObject rangeOfComposedCharacterSequenceAtIndex: index];
175
- BOOL isValidddatedIndex = range.location == index || NSMaxRange (range) == index;
176
- if (!isValidddatedIndex) {
177
- NSString *logString = [NSString stringWithFormat: @" 试图在 ComposedCharacterSequence 中间用 %@ 裁剪字符串,可能导致乱码、crash。原字符串为“%@ ”(%@ ),index 为 %@ ,命中的 ComposedCharacterSequence range 为 %@ " , NSStringFromSelector (originCMD), selfObject, @(selfObject.length), @(index), NSStringFromRange (range)];
178
- NSAssert (NO , logString);
179
- QMUILogWarn (@" QMUIStringSafety" , @" %@ " , logString);
180
- index = range.location ;
174
+ if (index < selfObject.length ) {
175
+ NSRange range = [selfObject rangeOfComposedCharacterSequenceAtIndex: index];
176
+ BOOL isValidddatedIndex = range.location == index || NSMaxRange (range) == index;
177
+ if (!isValidddatedIndex) {
178
+ NSString *logString = [NSString stringWithFormat: @" 试图在 ComposedCharacterSequence 中间用 %@ 裁剪字符串,可能导致乱码、crash。原字符串为“%@ ”(%@ ),index 为 %@ ,命中的 ComposedCharacterSequence range 为 %@ " , NSStringFromSelector (originCMD), selfObject, @(selfObject.length), @(index), NSStringFromRange (range)];
179
+ NSAssert (NO , logString);
180
+ QMUILogWarn (@" QMUIStringSafety" , @" %@ " , logString);
181
+ index = range.location ;
182
+ }
181
183
}
182
184
}
183
185
0 commit comments