Fix static analyzer issue with common indexes algorithm in _asdk_commonIndexesWithArray:

The issue was: The left operand of '>=' is a garbage value in else if check for lengths[i+1][j] ... I had to rewrite the algorithm to quiet the static analyzer.
This commit is contained in:
Michael Schneider
2016-04-18 14:38:32 -07:00
parent 103506beec
commit 9d07336c75
2 changed files with 66 additions and 15 deletions

View File

@@ -50,30 +50,36 @@
- (NSIndexSet *)_asdk_commonIndexesWithArray:(NSArray *)array compareBlock:(BOOL (^)(id lhs, id rhs))comparison
{
NSAssert(comparison != nil, @"Comparison block is required");
NSInteger lengths[self.count+1][array.count+1];
for (NSInteger i = self.count; i >= 0; i--) {
for (NSInteger j = array.count; j >= 0; j--) {
if (i == self.count || j == array.count) {
NSInteger selfCount = self.count;
NSInteger arrayCount = array.count;
NSInteger lengths[selfCount+1][arrayCount+1];
for (NSInteger i = 0; i <= selfCount; i++) {
for (NSInteger j = 0; j <= arrayCount; j++) {
if (i == 0 || j == 0) {
lengths[i][j] = 0;
} else if (comparison(self[i], array[j])) {
lengths[i][j] = 1 + lengths[i+1][j+1];
} else if (comparison(self[i-1], array[j-1])) {
lengths[i][j] = 1 + lengths[i-1][j-1];
} else {
lengths[i][j] = MAX(lengths[i+1][j], lengths[i][j+1]);
lengths[i][j] = MAX(lengths[i-1][j], lengths[i][j-1]);
}
}
}
NSMutableIndexSet *common = [NSMutableIndexSet indexSet];
for (NSInteger i = 0, j = 0; i < self.count && j < array.count;) {
if (comparison(self[i], array[j])) {
[common addIndex:i];
i++; j++;
} else if (lengths[i+1][j] >= lengths[i][j+1]) {
i++;
NSInteger i = selfCount, j = arrayCount;
while(i > 0 && j > 0) {
if (comparison(self[i-1], array[j-1])) {
[common addIndex:(i-1)];
i--; j--;
} else if (lengths[i-1][j] > lengths[i][j-1]) {
i--;
} else {
j++;
j--;
}
}
return common;
}