koan
koan

Reputation: 3686

Static analysis strange result

I was investigating the use of magic numbers and I have simplified my experiment to the following program. I noticed that when I use the static analysis function in Xcode I get the message "Left operand of '==' is a garbage value" for the temp[4] comparison. Is this a false positive and if so why ?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE *input;

  if(!(input = fopen("unknown_video.ext", "rb")))
    return(EXIT_FAILURE);

  int test_size = 10;

  int b = 0;
  int temp[test_size];

  while(b < test_size)
  {
    if((temp[b] = fgetc(input)) == EOF)
      break;

    b++;
  }

  fclose(input);

  if((temp[0] == 'R') && (temp[1] == 'I') && (temp[2]  == 'F') && (temp[3]  == 'F'))
  {
    printf("RIFF\n");
  }
  else if((temp[4] == 'f') && (temp[5] == 't') && (temp[6] == 'y') && (temp[7] == 'p'))
  {
    printf("QuickTime\n");
  }

  return(EXIT_SUCCESS);
}

Upvotes: 2

Views: 68

Answers (1)

dnaq
dnaq

Reputation: 2244

All values in your temp array are potentially unitialized.

Looking at the lines

while(b < test_size)
{
  if((temp[b] = fgetc(input)) == EOF)
    break;

  b++;
}

we have the possibility that fgetc returns EOF immediately, thus making all the following tests test against uninitialized values.

The easiest way to solve this is to initialize the temp array.

Upvotes: 4

Related Questions