text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE404_Improper_Resource_Shutdown__w32CreateFile_close_41.c
  Label Definition File: CWE404_Improper_Resource_Shutdown__w32CreateFile.label.xml
  Template File: source-sinks-41.tmpl.c
  */
  /*
   * @description
   * CWE: 404 Improper Resource Shutdown or Release
   * BadSource:  Open a file using CreateFile()
   * Sinks: close
   *    GoodSink: Close the file using CloseHandle()
   *    BadSink : Close the file using close()
   * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file
   *
   * */
  
  #include "std_testcase.h"
  
  #include <windows.h>
  
  #ifndef OMITBAD
  
  static void badSink(HANDLE data)
  {
      if (data != INVALID_HANDLE_VALUE)
      {
          /* FLAW: Attempt to close the file using close() instead of CloseHandle() */
          _close((int)data);
      }
  }
  
  void CWE404_Improper_Resource_Shutdown__w32CreateFile_close_41_bad()
  {
      HANDLE data;
      /* Initialize data */
      data = INVALID_HANDLE_VALUE;
      /* POTENTIAL FLAW: Open a file - need to make sure it is closed properly in the sink */
      data = CreateFile("BadSource_w32CreateFile.txt",
                        (GENERIC_WRITE|GENERIC_READ),
                        0,
                        NULL,
                        OPEN_ALWAYS,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
      badSink(data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  static void goodB2GSink(HANDLE data)
  {
      if (data != INVALID_HANDLE_VALUE)
      {
          /* FIX: Close the file using CloseHandle() */
          CloseHandle(data);
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  static void goodB2G()
  {
      HANDLE data;
      /* Initialize data */
      data = INVALID_HANDLE_VALUE;
      /* POTENTIAL FLAW: Open a file - need to make sure it is closed properly in the sink */
      data = CreateFile("BadSource_w32CreateFile.txt",
                        (GENERIC_WRITE|GENERIC_READ),
                        0,
                        NULL,
                        OPEN_ALWAYS,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
      goodB2GSink(data);
  }
  
  void CWE404_Improper_Resource_Shutdown__w32CreateFile_close_41_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()...");
      CWE404_Improper_Resource_Shutdown__w32CreateFile_close_41_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE404_Improper_Resource_Shutdown__w32CreateFile_close_41_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  