Hi James,
This is a courtesy email regarding Bug ID# 13107141.
Engineering has provided a workaround that you can use while we continue working on a resolution of the issue:
In this case, the analyzer does not know that both the loop which initializes the values and the loop that uses them will be executed the same number of times. You can hint to the analyzer that that is the case, by adding the following assert before the second loop:
assert(numbersIndex == numbersCount);
Alternatively, you could use numbersIndex instead if numbersCount in the second loop.
Thank you for your assistance in helping us discover and isolate bugs within our products.
Best Regards,
Developer Bug Reporting Team Apple Worldwide Developer Relations
####Original report:
#####GMT29-Jan-2013 18:31:53GMT #####Bug Title: Static analyzer false positive when iterating C array
See attached Xcode project. The main.c file is repeated below. The analyzer warns that "Assigned value is garbage or undefined" despite the fact that this can never be the case. The warning is at line 18:
NSUInteger integer = cNumbers[i];
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *objcNumbers = @[ @1, @2, @3 ];
NSUInteger numbersCount = objcNumbers.count;
NSUInteger *cNumbers = malloc(sizeof(NSUInteger) * numbersCount);
NSUInteger numbersIndex = 0;
for(NSNumber *number in objcNumbers) {
cNumbers[numbersIndex++] = [number unsignedIntegerValue];
}
for(NSUInteger i = 0; i < numbersCount; ++i) {
NSUInteger integer = cNumbers[i];
NSLog(@"Integer: %ld", (long)integer);
}
free(cNumbers);
}
return 0;
}