text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE190_Integer_Overflow__int64_t_max_square_73b.cpp
  Label Definition File: CWE190_Integer_Overflow.label.xml
  Template File: sources-sinks-73b.tmpl.cpp
  */
  /*
   * @description
   * CWE: 190 Integer Overflow
   * BadSource: max Set data to the max value for int64_t
   * GoodSource: Set data to a small, non-zero number (two)
   * Sinks: square
   *    GoodSink: Ensure there will not be an overflow before squaring data
   *    BadSink : Square data, which can lead to overflow
   * 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>
  
  #include <math.h>
  #include <inttypes.h>
  
  using namespace std;
  
  namespace CWE190_Integer_Overflow__int64_t_max_square_73
  {
  
  #ifndef OMITBAD
  
  void badSink(list<int64_t> dataList)
  {
      /* copy data out of dataList */
      int64_t data = dataList.back();
      {
          /* POTENTIAL FLAW: if (data*data) > LLONG_MAX, this will overflow */
          int64_t result = data * data;
          printLongLongLine(result);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(list<int64_t> dataList)
  {
      int64_t data = dataList.back();
      {
          /* POTENTIAL FLAW: if (data*data) > LLONG_MAX, this will overflow */
          int64_t result = data * data;
          printLongLongLine(result);
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void goodB2GSink(list<int64_t> dataList)
  {
      int64_t data = dataList.back();
      /* FIX: Add a check to prevent an overflow from occurring */
      if (imaxabs((intmax_t)data) <= sqrtl(LLONG_MAX))
      {
          int64_t result = data * data;
          printLongLongLine(result);
      }
      else
      {
          printLine("data value is too large to perform arithmetic safely.");
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  