text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE369_Divide_by_Zero__int_fgets_divide_16.c
  Label Definition File: CWE369_Divide_by_Zero__int.label.xml
  Template File: sources-sinks-16.tmpl.c
  */
  /*
   * @description
   * CWE: 369 Divide by Zero
   * BadSource: fgets Read data from the console using fgets()
   * GoodSource: Non-zero
   * Sinks: divide
   *    GoodSink: Check for zero before dividing
   *    BadSink : Divide a constant by data
   * Flow Variant: 16 Control flow: while(1)
   *
   * */
  
  #include "std_testcase.h"
  
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  #ifndef OMITBAD
  
  void CWE369_Divide_by_Zero__int_fgets_divide_16_bad()
  {
      int data;
      /* Initialize data */
      data = -1;
      while(1)
      {
          {
              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.");
              }
          }
          break;
      }
      while(1)
      {
          /* POTENTIAL FLAW: Possibly divide by zero */
          printIntLine(100 / data);
          break;
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */
  static void goodB2G()
  {
      int data;
      /* Initialize data */
      data = -1;
      while(1)
      {
          {
              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.");
              }
          }
          break;
      }
      while(1)
      {
          /* FIX: test for a zero denominator */
          if( data != 0 )
          {
              printIntLine(100 / data);
          }
          else
          {
              printLine("This would result in a divide by zero");
          }
          break;
      }
  }
  
  /* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */
  static void goodG2B()
  {
      int data;
      /* Initialize data */
      data = -1;
      while(1)
      {
          /* FIX: Use a value not equal to zero */
          data = 7;
          break;
      }
      while(1)
      {
          /* POTENTIAL FLAW: Possibly divide by zero */
          printIntLine(100 / data);
          break;
      }
  }
  
  void CWE369_Divide_by_Zero__int_fgets_divide_16_good()
  {
      goodB2G();
      goodG2B();
  }
  
  #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()...");
      CWE369_Divide_by_Zero__int_fgets_divide_16_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE369_Divide_by_Zero__int_fgets_divide_16_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  