text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE191_Integer_Underflow__int_fgets_sub_73a.cpp
  Label Definition File: CWE191_Integer_Underflow__int.label.xml
  Template File: sources-sinks-73a.tmpl.cpp
  */
  /*
   * @description
   * CWE: 191 Integer Underflow
   * BadSource: fgets Read data from the console using fgets()
   * GoodSource: Set data to a small, non-zero number (negative two)
   * Sinks: sub
   *    GoodSink: Ensure there will not be an underflow before subtracting 1 from data
   *    BadSink : Subtract 1 from data, which can cause an Underflow
   * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  #include <list>
  
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  using namespace std;
  
  namespace CWE191_Integer_Underflow__int_fgets_sub_73
  {
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void badSink(list<int> dataList);
  
  void bad()
  {
      int data;
      list<int> dataList;
      /* Initialize data */
      data = 0;
      {
          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.");
          }
      }
      /* Put data in a list */
      dataList.push_back(data);
      dataList.push_back(data);
      dataList.push_back(data);
      badSink(dataList);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(list<int> dataList);
  
  static void goodG2B()
  {
      int data;
      list<int> dataList;
      /* Initialize data */
      data = 0;
      /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */
      data = -2;
      /* Put data in a list */
      dataList.push_back(data);
      dataList.push_back(data);
      dataList.push_back(data);
      goodG2BSink(dataList);
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void goodB2GSink(list<int> dataList);
  
  static void goodB2G()
  {
      int data;
      list<int> dataList;
      /* Initialize data */
      data = 0;
      {
          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.");
          }
      }
      dataList.push_back(data);
      dataList.push_back(data);
      dataList.push_back(data);
      goodB2GSink(dataList);
  }
  
  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 CWE191_Integer_Underflow__int_fgets_sub_73; /* 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
  