text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE190_Integer_Overflow__int_fgets_square_64b.c
  Label Definition File: CWE190_Integer_Overflow__int.label.xml
  Template File: sources-sinks-64b.tmpl.c
  */
  /*
   * @description
   * CWE: 190 Integer Overflow
   * BadSource: fgets Read data from the console using fgets()
   * 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: 64 Data flow: void pointer to data passed from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  #include <math.h>
  
  #ifndef OMITBAD
  
  void CWE190_Integer_Overflow__int_fgets_square_64b_badSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      int * dataPtr = (int *)dataVoidPtr;
      /* dereference dataPtr into data */
      int data = (*dataPtr);
      {
          /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */
          int result = data * data;
          printIntLine(result);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE190_Integer_Overflow__int_fgets_square_64b_goodG2BSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      int * dataPtr = (int *)dataVoidPtr;
      /* dereference dataPtr into data */
      int data = (*dataPtr);
      {
          /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */
          int result = data * data;
          printIntLine(result);
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE190_Integer_Overflow__int_fgets_square_64b_goodB2GSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      int * dataPtr = (int *)dataVoidPtr;
      /* dereference dataPtr into data */
      int data = (*dataPtr);
      /* FIX: Add a check to prevent an overflow from occurring */
      if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))
      {
          int result = data * data;
          printIntLine(result);
      }
      else
      {
          printLine("data value is too large to perform arithmetic safely.");
      }
  }
  
  #endif /* OMITGOOD */
  