text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE675_Duplicate_Operations_on_Resource__w32CreateFile_09.c
  Label Definition File: CWE675_Duplicate_Operations_on_Resource__w32CreateFile.label.xml
  Template File: sources-sinks-09.tmpl.c
  */
  /*
   * @description
   * CWE: 675 Duplicate Operations on Resource
   * BadSource:  Open and close a file using CreateFile() and CloseHandle()
   * GoodSource: Open a file using CreateFile()
   * Sinks:
   *    GoodSink: Do nothing
   *    BadSink : Close the file
   * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <windows.h>
  
  #ifndef OMITBAD
  
  void CWE675_Duplicate_Operations_on_Resource__w32CreateFile_09_bad()
  {
      HANDLE data;
      data = INVALID_HANDLE_VALUE; /* Initialize data */
      if(GLOBAL_CONST_TRUE)
      {
          data = CreateFile("BadSource_w32CreateFile.txt",
                            (GENERIC_WRITE|GENERIC_READ),
                            0,
                            NULL,
                            OPEN_ALWAYS,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);
          /* POTENTIAL FLAW: Close the file in the source */
          CloseHandle(data);
      }
      if(GLOBAL_CONST_TRUE)
      {
          /* POTENTIAL FLAW: Close the file in the sink (it may have been closed in the Source) */
          CloseHandle(data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */
  static void goodB2G1()
  {
      HANDLE data;
      data = INVALID_HANDLE_VALUE; /* Initialize data */
      if(GLOBAL_CONST_TRUE)
      {
          data = CreateFile("BadSource_w32CreateFile.txt",
                            (GENERIC_WRITE|GENERIC_READ),
                            0,
                            NULL,
                            OPEN_ALWAYS,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);
          /* POTENTIAL FLAW: Close the file in the source */
          CloseHandle(data);
      }
      if(GLOBAL_CONST_FALSE)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* Do nothing */
          /* FIX: Don't close the file in the sink */
          ; /* empty statement needed for some flow variants */
      }
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  static void goodB2G2()
  {
      HANDLE data;
      data = INVALID_HANDLE_VALUE; /* Initialize data */
      if(GLOBAL_CONST_TRUE)
      {
          data = CreateFile("BadSource_w32CreateFile.txt",
                            (GENERIC_WRITE|GENERIC_READ),
                            0,
                            NULL,
                            OPEN_ALWAYS,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);
          /* POTENTIAL FLAW: Close the file in the source */
          CloseHandle(data);
      }
      if(GLOBAL_CONST_TRUE)
      {
          /* Do nothing */
          /* FIX: Don't close the file in the sink */
          ; /* empty statement needed for some flow variants */
      }
  }
  
  /* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */
  static void goodG2B1()
  {
      HANDLE data;
      data = INVALID_HANDLE_VALUE; /* Initialize data */
      if(GLOBAL_CONST_FALSE)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: Open, but do not close the file in the source */
          data = CreateFile("GoodSource_w32CreateFile.txt",
                            (GENERIC_WRITE|GENERIC_READ),
                            0,
                            NULL,
                            OPEN_ALWAYS,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);
      }
      if(GLOBAL_CONST_TRUE)
      {
          /* POTENTIAL FLAW: Close the file in the sink (it may have been closed in the Source) */
          CloseHandle(data);
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  static void goodG2B2()
  {
      HANDLE data;
      data = INVALID_HANDLE_VALUE; /* Initialize data */
      if(GLOBAL_CONST_TRUE)
      {
          /* FIX: Open, but do not close the file in the source */
          data = CreateFile("GoodSource_w32CreateFile.txt",
                            (GENERIC_WRITE|GENERIC_READ),
                            0,
                            NULL,
                            OPEN_ALWAYS,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);
      }
      if(GLOBAL_CONST_TRUE)
      {
          /* POTENTIAL FLAW: Close the file in the sink (it may have been closed in the Source) */
          CloseHandle(data);
      }
  }
  
  void CWE675_Duplicate_Operations_on_Resource__w32CreateFile_09_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()...");
      CWE675_Duplicate_Operations_on_Resource__w32CreateFile_09_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE675_Duplicate_Operations_on_Resource__w32CreateFile_09_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  