text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE400_Resource_Exhaustion__fgets_fwrite_81a.cpp
  Label Definition File: CWE400_Resource_Exhaustion.label.xml
  Template File: sources-sinks-81a.tmpl.cpp
  */
  /*
   * @description
   * CWE: 400 Resource Exhaustion
   * BadSource: fgets Read data from the console using fgets()
   * GoodSource: Assign count to be a relatively small number
   * Sinks: fwrite
   *    GoodSink: Write to a file count number of times, but first validate count
   *    BadSink : Write to a file count number of times
   * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference
   *
   * */
  
  #include "std_testcase.h"
  #include "CWE400_Resource_Exhaustion__fgets_fwrite_81.h"
  
  #define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)
  
  namespace CWE400_Resource_Exhaustion__fgets_fwrite_81
  {
  
  #ifndef OMITBAD
  
  void bad()
  {
      int count;
      /* Initialize count */
      count = -1;
      {
          char inputBuffer[CHAR_ARRAY_SIZE] = "";
          /* POTENTIAL FLAW: Read count from the console using fgets() */
          if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)
          {
              /* Convert to int */
              count = atoi(inputBuffer);
          }
          else
          {
              printLine("fgets() failed.");
          }
      }
      const CWE400_Resource_Exhaustion__fgets_fwrite_81_base& baseObject = CWE400_Resource_Exhaustion__fgets_fwrite_81_bad();
      baseObject.action(count);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  static void goodG2B()
  {
      int count;
      /* Initialize count */
      count = -1;
      /* FIX: Use a relatively small number */
      count = 20;
      const CWE400_Resource_Exhaustion__fgets_fwrite_81_base& baseObject = CWE400_Resource_Exhaustion__fgets_fwrite_81_goodG2B();
      baseObject.action(count);
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  static void goodB2G()
  {
      int count;
      /* Initialize count */
      count = -1;
      {
          char inputBuffer[CHAR_ARRAY_SIZE] = "";
          /* POTENTIAL FLAW: Read count from the console using fgets() */
          if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)
          {
              /* Convert to int */
              count = atoi(inputBuffer);
          }
          else
          {
              printLine("fgets() failed.");
          }
      }
      const CWE400_Resource_Exhaustion__fgets_fwrite_81_base& baseObject = CWE400_Resource_Exhaustion__fgets_fwrite_81_goodB2G();
      baseObject.action(count);
  }
  
  void good()
  {
      goodG2B();
      goodB2G();
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  
  /* 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
  
  using namespace CWE400_Resource_Exhaustion__fgets_fwrite_81; /* so that we can use good and bad easily */
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  