text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE617_Reachable_Assertion__fgets_09.c
  Label Definition File: CWE617_Reachable_Assertion.label.xml
  Template File: sources-sink-09.tmpl.c
  */
  /*
   * @description
   * CWE: 617 Reachable Assertion
   * BadSource: fgets Read data from the console using fgets()
   * GoodSource: Number greater than ASSERT_VALUE
   * Sink:
   *    BadSink : Assert if n is less than or equal to ASSERT_VALUE
   * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <assert.h>
  
  #define ASSERT_VALUE 5
  
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  #ifndef OMITBAD
  
  void CWE617_Reachable_Assertion__fgets_09_bad()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(GLOBAL_CONST_TRUE)
      {
          {
              char inputBuffer[CHAR_ARRAY_SIZE] = "";
              /* POTENTIAL FLAW: Read data from the console using fgets() */
              if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)
              {
                  /* Convert to int */
                  data = atoi(inputBuffer);
              }
              else
              {
                  printLine("fgets() failed.");
              }
          }
      }
      /* POTENTIAL FLAW: this assertion could trigger if n <= ASSERT_VALUE */
      assert(data > ASSERT_VALUE);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */
  static void goodG2B1()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(GLOBAL_CONST_FALSE)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: Use a value greater than the assert value */
          data = ASSERT_VALUE+1;
      }
      /* POTENTIAL FLAW: this assertion could trigger if n <= ASSERT_VALUE */
      assert(data > ASSERT_VALUE);
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(GLOBAL_CONST_TRUE)
      {
          /* FIX: Use a value greater than the assert value */
          data = ASSERT_VALUE+1;
      }
      /* POTENTIAL FLAW: this assertion could trigger if n <= ASSERT_VALUE */
      assert(data > ASSERT_VALUE);
  }
  
  void CWE617_Reachable_Assertion__fgets_09_good()
  {
      goodG2B1();
      goodG2B2();
  }
  
  #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()...");
      CWE617_Reachable_Assertion__fgets_09_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE617_Reachable_Assertion__fgets_09_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  