text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE773_Missing_Reference_to_Active_File_Descriptor_or_Handle__w32CreateFile_01.c
  Label Definition File: CWE773_Missing_Reference_to_Active_File_Descriptor_or_Handle__w32CreateFile.label.xml
  Template File: source-sinks-01.tmpl.c
  */
  /*
   * @description
   * CWE: 773 Missing Reference to Active File Descriptor or Handle
   * BadSource:  Create a file handle using CreateFile()
   * Sinks:
   *    GoodSink: Close the file handle before reusing it
   *    BadSink : Reassign the file handle before closing it
   * Flow Variant: 01 Baseline
   *
   * */
  
  #include "std_testcase.h"
  
  #include <windows.h>
  
  #ifndef OMITBAD
  
  void CWE773_Missing_Reference_to_Active_File_Descriptor_or_Handle__w32CreateFile_01_bad()
  {
      HANDLE data;
      /* Initialize data */
      data = INVALID_HANDLE_VALUE;
      /* POTENTIAL FLAW: Create a file handle using CreateFile() that may not be closed properly */
      data = CreateFile("BadSource_w32CreateFile.txt",
                        (GENERIC_WRITE|GENERIC_READ),
                        0,
                        NULL,
                        OPEN_ALWAYS,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
      /* FLAW: Point data to another file handle without closing the handle from the source */
      data = CreateFile("BadSink_w32CreateFile.txt",
                        (GENERIC_WRITE|GENERIC_READ),
                        0,
                        NULL,
                        OPEN_ALWAYS,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
      /* avoid incidental for not closing the file handle */
      if (data != INVALID_HANDLE_VALUE)
      {
          CloseHandle(data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G() uses the BadSource with the GoodSink */
  static void goodB2G()
  {
      HANDLE data;
      /* Initialize data */
      data = INVALID_HANDLE_VALUE;
      /* POTENTIAL FLAW: Create a file handle using CreateFile() that may not be closed properly */
      data = CreateFile("BadSource_w32CreateFile.txt",
                        (GENERIC_WRITE|GENERIC_READ),
                        0,
                        NULL,
                        OPEN_ALWAYS,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
      /* FIX: Close the file from the source before pointing data to a new file handle */
      if (data != INVALID_HANDLE_VALUE)
      {
          CloseHandle(data);
      }
      data = CreateFile("GoodSink_w32CreateFile.txt",
                        (GENERIC_WRITE|GENERIC_READ),
                        0,
                        NULL,
                        OPEN_ALWAYS,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
      /* avoid incidental for not closing the file handle */
      if (data != INVALID_HANDLE_VALUE)
      {
          CloseHandle(data);
      }
  }
  
  void CWE773_Missing_Reference_to_Active_File_Descriptor_or_Handle__w32CreateFile_01_good()
  {
      goodB2G();
  }
  
  #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()...");
      CWE773_Missing_Reference_to_Active_File_Descriptor_or_Handle__w32CreateFile_01_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE773_Missing_Reference_to_Active_File_Descriptor_or_Handle__w32CreateFile_01_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  