text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE416_Use_After_Free__malloc_free_long_long_17.c
  Label Definition File: CWE416_Use_After_Free__malloc_free.label.xml
  Template File: sources-sinks-17.tmpl.c
  */
  /*
   * @description
   * CWE: 416 Use After Free
   * BadSource:  Allocate data using malloc(), initialize memory block, and Deallocate data using free()
   * GoodSource: Allocate data using malloc() and initialize memory block
   * Sinks:
   *    GoodSink: Do nothing
   *    BadSink : Use data
   * Flow Variant: 17 Control flow: for loops
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifndef OMITBAD
  
  void CWE416_Use_After_Free__malloc_free_long_long_17_bad()
  {
      int h,i,j,k;
      long long * data;
      /* Initialize data */
      data = NULL;
      for(h = 0; h < 0; h++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          data = (long long *)malloc(100*sizeof(long long));
          {
              size_t i;
              for(i = 0; i < 100; i++)
              {
                  data[i] = 5L;
              }
          }
          /* FIX: Do not delete data in the source */
      }
      for(i = 0; i < 1; i++)
      {
          data = (long long *)malloc(100*sizeof(long long));
          {
              size_t i;
              for(i = 0; i < 100; i++)
              {
                  data[i] = 5L;
              }
          }
          /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */
          free(data);
      }
      for(j = 0; j < 1; j++)
      {
          /* POTENTIAL FLAW - Use of data that may have been deleted */
          printLongLongLine(data[0]);
          /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */
      }
      for(k = 0; k < 0; k++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX - Don't use data that may have been deleted already */
          /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */
          /* do nothing */
          ; /* empty statement needed for some flow variants */
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G() - use badsource and goodsink by changing the conditions on the third and fourth for statements */
  static void goodB2G()
  {
      int h,i,j,k;
      long long * data;
      /* Initialize data */
      data = NULL;
      for(h = 0; h < 0; h++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          data = (long long *)malloc(100*sizeof(long long));
          {
              size_t i;
              for(i = 0; i < 100; i++)
              {
                  data[i] = 5L;
              }
          }
          /* FIX: Do not delete data in the source */
      }
      for(i = 0; i < 1; i++)
      {
          data = (long long *)malloc(100*sizeof(long long));
          {
              size_t i;
              for(i = 0; i < 100; i++)
              {
                  data[i] = 5L;
              }
          }
          /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */
          free(data);
      }
      for(j = 0; j < 0; j++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* POTENTIAL FLAW - Use of data that may have been deleted */
          printLongLongLine(data[0]);
          /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */
      }
      for(k = 0; k < 1; k++)
      {
          /* FIX - Don't use data that may have been deleted already */
          /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */
          /* do nothing */
          ; /* empty statement needed for some flow variants */
      }
  }
  
  /* goodG2B() - use goodsource and badsink by changing the conditions on the first and second for statements */
  static void goodG2B()
  {
      int h,i,j,k;
      long long * data;
      /* Initialize data */
      data = NULL;
      for(h = 0; h < 1; h++)
      {
          data = (long long *)malloc(100*sizeof(long long));
          {
              size_t i;
              for(i = 0; i < 100; i++)
              {
                  data[i] = 5L;
              }
          }
          /* FIX: Do not delete data in the source */
      }
      for(i = 0; i < 0; i++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          data = (long long *)malloc(100*sizeof(long long));
          {
              size_t i;
              for(i = 0; i < 100; i++)
              {
                  data[i] = 5L;
              }
          }
          /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */
          free(data);
      }
      for(j = 0; j < 1; j++)
      {
          /* POTENTIAL FLAW - Use of data that may have been deleted */
          printLongLongLine(data[0]);
          /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */
      }
      for(k = 0; k < 0; k++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX - Don't use data that may have been deleted already */
          /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */
          /* do nothing */
          ; /* empty statement needed for some flow variants */
      }
  }
  
  void CWE416_Use_After_Free__malloc_free_long_long_17_good()
  {
      goodB2G();
      goodG2B();
  }
  
  #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()...");
      CWE416_Use_After_Free__malloc_free_long_long_17_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE416_Use_After_Free__malloc_free_long_long_17_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  