Why does the static analyzer warn of a garbage value in this code?

With Apple LLVM 4.2 (XCode 4.6), the static analyzer warns "The left operand of '>' is a garbage value" in this class's 'update' method:

Test.h

#import <Foundation/Foundation.h>

typedef struct {
    float x;
} TestInnerStruct;

typedef struct {
    TestInnerStruct innerStruct;
    int value;
} TestOuterStruct;

@interface Test : NSObject {
    TestOuterStruct outerStruct;
}

@end

Test.m

#import "Test.h"

@implementation Test

- (id) init {
    if (self = [super init]) {
        outerStruct.value = 1;
    }
    return self;
}

- (void) update {
    outerStruct.innerStruct = (TestInnerStruct){0.0f};
    if (outerStruct.value > 0) {
        NSLog(@"Value greater than zero");
    }
}

@end

This is a contrived class, made by cutting down the real class to the minimum needed to reproduce this static analyzer warning. In the real class, there are good reasons for using the nested structs.

What path through the code would cause that operand to be a garbage value? Or is the static analyzer getting confused?

Edit for clarification: It's not just that the analyzer's considering the case where [super init] returns nil. I know that because the warning goes away on commenting out the first line of the update method.


Eric is right, and it is apparently a compiler issue: If one changes the definition of TestOuterStruct to

typedef struct {
    int value;
    TestInnerStruct innerStruct;
} TestOuterStruct;

ie, if the two elements are interchanged, the code compiles without error.


The outerStruct.value is only set to 1, if self = [super init] succeeds. If is does not (which is of course a VERY rare event, but possible, otherwise the if would not be necessary), the value is not set but garbage.

链接地址: http://www.djcxy.com/p/43740.html

上一篇: 在matplotlib中的交互模式

下一篇: 为什么静态分析器在此代码中警告垃圾值?