text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE690_NULL_Deref_from_Return__fopen_09.c
  Label Definition File: CWE690_NULL_Deref_from_Return.fclose.label.xml
  Template File: source-sinks-09.tmpl.c
  */
  /*
   * @description
   * CWE: 690 Unchecked Return Value To NULL Pointer
   * BadSource: fopen Open data with fopen()
   * Sinks: 0
   *    GoodSink: Check data for NULL
   *    BadSink : Do not check data for NULL
   * Flow Variant: 09 Control flow: if(global_const_t) and if(global_const_f)
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef OMITBAD
  
  void CWE690_NULL_Deref_from_Return__fopen_09_bad()
  {
      FILE * data;
      /* Initialize data */
      data = NULL;
      data = fopen("file.txt", "w+");
      if(global_const_t)
      {
          /* FLAW: if the fopen failed, data could be NULL here */
          fclose(data);
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: check the return value */
          if (data != NULL)
          {
              fclose(data);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G1() - use badsource and goodsink by changing global_const_t to global_const_f */
  static void goodB2G1()
  {
      FILE * data;
      /* Initialize data */
      data = NULL;
      data = fopen("file.txt", "w+");
      if(global_const_f)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FLAW: if the fopen failed, data could be NULL here */
          fclose(data);
      }
      else
      {
          /* FIX: check the return value */
          if (data != NULL)
          {
              fclose(data);
          }
      }
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing statements in if  */
  static void goodB2G2()
  {
      FILE * data;
      /* Initialize data */
      data = NULL;
      data = fopen("file.txt", "w+");
      if(global_const_t)
      {
          /* FIX: check the return value */
          if (data != NULL)
          {
              fclose(data);
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FLAW: if the fopen failed, data could be NULL here */
          fclose(data);
      }
  }
  
  void CWE690_NULL_Deref_from_Return__fopen_09_good()
  {
      goodB2G1();
      goodB2G2();
  }
  
  #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()...");
      CWE690_NULL_Deref_from_Return__fopen_09_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE690_NULL_Deref_from_Return__fopen_09_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  