text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE404_Improper_Resource_Shutdown__freopen_close_18.c
  Label Definition File: CWE404_Improper_Resource_Shutdown.label.xml
  Template File: source-sinks-18.tmpl.c
  */
  /*
   * @description
   * CWE: 404 Improper Resource Shutdown or Release
   * BadSource: freopen Open a file using freopen()
   * Sinks: close
   *    GoodSink: Close the file using fclose()
   *    BadSink : Close the file using close()
   * Flow Variant: 18 Control flow: goto statements
   *
   * */
  
  #include "std_testcase.h"
  
  #ifdef _WIN32
  # define CLOSE _close
  #else
  # define CLOSE close
  #endif
  
  #ifndef OMITBAD
  
  void CWE404_Improper_Resource_Shutdown__freopen_close_18_bad()
  {
      FILE * data;
      /* Initialize data */
      data = NULL;
      data = freopen("BadSource_freopen.txt","w+",stdin);
      goto sink;
      /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
      if (data != NULL)
      {
          /* FIX: Close the file using fclose() */
          fclose(data);
      }
  sink:
      if (data != NULL)
      {
          /* FLAW: Attempt to close the file using close() instead of fclose() */
          CLOSE((int)data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G() - use badsource and goodsink by reversing the blocks on the goto statement */
  static void goodB2G()
  {
      FILE * data;
      /* Initialize data */
      data = NULL;
      data = freopen("BadSource_freopen.txt","w+",stdin);
      goto sink;
      /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
      if (data != NULL)
      {
          /* FLAW: Attempt to close the file using close() instead of fclose() */
          CLOSE((int)data);
      }
  sink:
      if (data != NULL)
      {
          /* FIX: Close the file using fclose() */
          fclose(data);
      }
  }
  
  void CWE404_Improper_Resource_Shutdown__freopen_close_18_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_close_18_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE404_Improper_Resource_Shutdown__freopen_close_18_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  