1
votes

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?

2
Are "outputBuffer" and "outputLength" initialized in all cases?zaph
Hrmm I didnt even know they were the issue. Looking at the code. They are both declared in another method further up in the code. However it dose not seem that they are global vars.HurkNburkS
declaration is not initialization. int i; is not initialized int i = 0; is. clear?Brad Allred
Thanks. then the next question is how would I know the outputlength..HurkNburkS
Provide the API you are calling, outputLength may be an output parameter.zaph

2 Answers

5
votes

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.

1
votes

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.