text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE191_Integer_Underflow__int64_t_fscanf_multiply_72a.cpp
  Label Definition File: CWE191_Integer_Underflow.label.xml
  Template File: sources-sinks-72a.tmpl.cpp
  */
  /*
   * @description
   * CWE: 191 Integer Underflow
   * BadSource: fscanf Read data from the console using fscanf()
   * GoodSource: Set data to a small, non-zero number (negative two)
   * Sinks: multiply
   *    GoodSink: Ensure there will not be an underflow before multiplying data by 2
   *    BadSink : If data is negative, multiply by 2, which can cause an underflow
   * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files
   *
   * */
  
  #include <inttypes.h>
  #include "std_testcase.h"
  #include <vector>
  
  using namespace std;
  
  namespace CWE191_Integer_Underflow__int64_t_fscanf_multiply_72
  {
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void badSink(vector<int64_t> dataVector);
  
  void bad()
  {
      int64_t data;
      vector<int64_t> dataVector;
      data = 0LL;
      /* POTENTIAL FLAW: Use a value input from the console */
      fscanf (stdin, "%" SCNd64, &data);
      /* Put data in a vector */
      dataVector.insert(dataVector.end(), 1, data);
      dataVector.insert(dataVector.end(), 1, data);
      dataVector.insert(dataVector.end(), 1, data);
      badSink(dataVector);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(vector<int64_t> dataVector);
  
  static void goodG2B()
  {
      int64_t data;
      vector<int64_t> dataVector;
      data = 0LL;
      /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */
      data = -2;
      /* Put data in a vector */
      dataVector.insert(dataVector.end(), 1, data);
      dataVector.insert(dataVector.end(), 1, data);
      dataVector.insert(dataVector.end(), 1, data);
      goodG2BSink(dataVector);
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void goodB2GSink(vector<int64_t> dataVector);
  
  static void goodB2G()
  {
      int64_t data;
      vector<int64_t> dataVector;
      data = 0LL;
      /* POTENTIAL FLAW: Use a value input from the console */
      fscanf (stdin, "%" SCNd64, &data);
      dataVector.insert(dataVector.end(), 1, data);
      dataVector.insert(dataVector.end(), 1, data);
      dataVector.insert(dataVector.end(), 1, data);
      goodB2GSink(dataVector);
  }
  
  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__int64_t_fscanf_multiply_72; /* 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
  