text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE404_Improper_Resource_Shutdown__freopen_w32CloseHandle_44.c
  Label Definition File: CWE404_Improper_Resource_Shutdown.label.xml
  Template File: source-sinks-44.tmpl.c
  */
  /*
   * @description
   * CWE: 404 Improper Resource Shutdown or Release
   * BadSource: freopen Open a file using freopen()
   * Sinks: w32CloseHandle
   *    GoodSink: Close the file using fclose()
   *    BadSink : Close the file using CloseHandle
   * Flow Variant: 44 Data/control flow: data passed as an argument from one function to a function in the same source file called via a function pointer
   *
   * */
  
  #include "std_testcase.h"
  
  #include <windows.h>
  
  #ifndef OMITBAD
  
  static void badSink(FILE * data)
  {
      if (data != NULL)
      {
          /* FLAW: Attempt to close the file using CloseHandle() instead of fclose() */
          CloseHandle((HANDLE)data);
      }
  }
  
  void CWE404_Improper_Resource_Shutdown__freopen_w32CloseHandle_44_bad()
  {
      FILE * data;
      /* define a function pointer */
      void (*funcPtr) (FILE *) = badSink;
      /* Initialize data */
      data = NULL;
      /* POTENTIAL FLAW: Open a file - need to make sure it is closed properly in the sink */
      data = freopen("BadSource_freopen.txt","w+",stdin);
      /* use the function pointer */
      funcPtr(data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G() uses the BadSource with the GoodSink */
  static void goodB2GSink(FILE * data)
  {
      if (data != NULL)
      {
          /* FIX: Close the file using fclose() */
          fclose(data);
      }
  }
  
  static void goodB2G()
  {
      FILE * data;
      void (*funcPtr) (FILE *) = goodB2GSink;
      /* Initialize data */
      data = NULL;
      /* POTENTIAL FLAW: Open a file - need to make sure it is closed properly in the sink */
      data = freopen("BadSource_freopen.txt","w+",stdin);
      funcPtr(data);
  }
  
  void CWE404_Improper_Resource_Shutdown__freopen_w32CloseHandle_44_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__freopen_w32CloseHandle_44_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE404_Improper_Resource_Shutdown__freopen_w32CloseHandle_44_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  