text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08.c
  Label Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml
  Template File: sources-sinks-08.tmpl.c
  */
  /*
   * @description
   * CWE: 457 Use of Uninitialized Variable
   * BadSource: partial_init Initialize part, but not all of the array
   * GoodSource: Initialize data
   * Sinks: use
   *    GoodSink: Initialize then use data
   *    BadSink : Use data
   * Flow Variant: 08 Control flow: if(static_returns_t()) and if(static_returns_f())
   *
   * */
  
  #include "std_testcase.h"
  
  /* The two function below always return the same value, so a tool
     should be able to identify that calls to the functions will always
     return a fixed value. */
  static int static_returns_t()
  {
      return 1;
  }
  
  static int static_returns_f()
  {
      return 0;
  }
  
  #ifndef OMITBAD
  
  void CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08_bad()
  {
      int * data;
      data = (int *)malloc(10*sizeof(int));
      if(static_returns_t())
      {
          {
              int i;
              for(i=0; i<(10/2); i++)
              {
                  data[i] = i;
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int i;
              for(i=0; i<10; i++)
              {
                  data[i] = i;
              }
          }
      }
      if(static_returns_t())
      {
          {
              int i;
              for(i=0; i<10; i++)
              {
                  printIntLine(data[i]);
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int i;
              for(i=0; i<10; i++)
              {
                  data[i] = i;
              }
          }
          {
              int i;
              for(i=0; i<10; i++)
              {
                  printIntLine(data[i]);
              }
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G1() - use badsource and goodsink by changing the second static_returns_t() to static_returns_f() */
  static void goodB2G1()
  {
      int * data;
      data = (int *)malloc(10*sizeof(int));
      if(static_returns_t())
      {
          {
              int i;
              for(i=0; i<(10/2); i++)
              {
                  data[i] = i;
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int i;
              for(i=0; i<10; i++)
              {
                  data[i] = i;
              }
          }
      }
      if(static_returns_f())
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int i;
              for(i=0; i<10; i++)
              {
                  printIntLine(data[i]);
              }
          }
      }
      else
      {
          {
              int i;
              for(i=0; i<10; i++)
              {
                  data[i] = i;
              }
          }
          {
              int i;
              for(i=0; i<10; i++)
              {
                  printIntLine(data[i]);
              }
          }
      }
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  static void goodB2G2()
  {
      int * data;
      data = (int *)malloc(10*sizeof(int));
      if(static_returns_t())
      {
          {
              int i;
              for(i=0; i<(10/2); i++)
              {
                  data[i] = i;
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int i;
              for(i=0; i<10; i++)
              {
                  data[i] = i;
              }
          }
      }
      if(static_returns_t())
      {
          {
              int i;
              for(i=0; i<10; i++)
              {
                  data[i] = i;
              }
          }
          {
              int i;
              for(i=0; i<10; i++)
              {
                  printIntLine(data[i]);
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int i;
              for(i=0; i<10; i++)
              {
                  printIntLine(data[i]);
              }
          }
      }
  }
  
  /* goodG2B1() - use goodsource and badsink by changing the first static_returns_t() to static_returns_f() */
  static void goodG2B1()
  {
      int * data;
      data = (int *)malloc(10*sizeof(int));
      if(static_returns_f())
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int i;
              for(i=0; i<(10/2); i++)
              {
                  data[i] = i;
              }
          }
      }
      else
      {
          {
              int i;
              for(i=0; i<10; i++)
              {
                  data[i] = i;
              }
          }
      }
      if(static_returns_t())
      {
          {
              int i;
              for(i=0; i<10; i++)
              {
                  printIntLine(data[i]);
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int i;
              for(i=0; i<10; i++)
              {
                  data[i] = i;
              }
          }
          {
              int i;
              for(i=0; i<10; i++)
              {
                  printIntLine(data[i]);
              }
          }
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  static void goodG2B2()
  {
      int * data;
      data = (int *)malloc(10*sizeof(int));
      if(static_returns_t())
      {
          {
              int i;
              for(i=0; i<10; i++)
              {
                  data[i] = i;
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int i;
              for(i=0; i<(10/2); i++)
              {
                  data[i] = i;
              }
          }
      }
      if(static_returns_t())
      {
          {
              int i;
              for(i=0; i<10; i++)
              {
                  printIntLine(data[i]);
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int i;
              for(i=0; i<10; i++)
              {
                  data[i] = i;
              }
          }
          {
              int i;
              for(i=0; i<10; i++)
              {
                  printIntLine(data[i]);
              }
          }
      }
  }
  
  void CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08_good()
  {
      goodB2G1();
      goodB2G2();
      goodG2B1();
      goodG2B2();
  }
  
  #endif /* OMITGOOD */
  
  /* Below is the main(). It is only used when building this testcase on
     its own for testing or for building a binary to use in testing binary
     analysis tools. It is not used when compiling all the testcases as one
     application, which is how source code analysis tools are tested. */
  
  #ifdef INCLUDEMAIN
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  