text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE190_Integer_Overflow__short_fscanf_square_52c.c
  Label Definition File: CWE190_Integer_Overflow.label.xml
  Template File: sources-sinks-52c.tmpl.c
  */
  /*
   * @description
   * CWE: 190 Integer Overflow
   * BadSource: fscanf Read data from the console using fscanf()
   * 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: 52 Data flow: data passed as an argument from one function to another to another in three different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #include <math.h>
  
  #ifndef OMITBAD
  
  void CWE190_Integer_Overflow__short_fscanf_square_52c_badSink(short data)
  {
      {
          /* POTENTIAL FLAW: if (data*data) > SHRT_MAX, this will overflow */
          short result = data * data;
          printIntLine(result);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE190_Integer_Overflow__short_fscanf_square_52c_goodG2BSink(short data)
  {
      {
          /* POTENTIAL FLAW: if (data*data) > SHRT_MAX, this will overflow */
          short result = data * data;
          printIntLine(result);
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE190_Integer_Overflow__short_fscanf_square_52c_goodB2GSink(short data)
  {
      /* FIX: Add a check to prevent an overflow from occurring */
      if (abs((long)data) <= (long)sqrt((double)SHRT_MAX))
      {
          short result = data * data;
          printIntLine(result);
      }
      else
      {
          printLine("data value is too large to perform arithmetic safely.");
      }
  }
  
  #endif /* OMITGOOD */
  