Question

I got this Analyzer warning

Argument in message expression is an uninitialized value

with a little orange arrow under the second square bracket on the second line of the following code

- (NSString *)base64EncodedString
{
    size_t outputLength;
    char *outputBuffer =
        NewBase64Encode([self bytes], [self length], true, &outputLength);



    NSString *result =
        [[[NSString alloc]
            initWithBytes:outputBuffer
            length:outputLength
            encoding:NSASCIIStringEncoding]
        autorelease];
    free(outputBuffer);
    return result;
}

I am not sure what is causing this I have tried several things but never have I had this type of Analyzer warning.

How do I get rid of this Analyzer warning?

Was it helpful?

Solution

When you declare

size_t outputLength;

you are not initializing it. The analyzer does not know that NewBase64Encode will set its value, so it's warning you that something might go wrong.

If you are absolutely 100% sure that the value will be initialized later on, then you can safely ignore the analyzer warning, but this is rarely the case. For instance, NewBase64Encode might fail and outputLength will result uninitialized, leading to undefined behavior.

Is always a good practice to initialize every variable you declare, so in the specific case just do

size_t outputLength = 0;

The analyzer will be happy and your code safer.

OTHER TIPS

The warning says exactly the reason:

one of your used arguments is uninitialized. If you explicitely set outputBuffer, outputLength to some default value, the warning will go away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top