text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE190_Integer_Overflow__char_fscanf_multiply_62a.cpp
  Label Definition File: CWE190_Integer_Overflow.label.xml
  Template File: sources-sinks-62a.tmpl.cpp
  */
  /*
   * @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: multiply
   *    GoodSink: Ensure there will not be an overflow before multiplying data by 2
   *    BadSink : If data is positive, multiply by 2, which can cause an overflow
   * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  namespace CWE190_Integer_Overflow__char_fscanf_multiply_62
  {
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void badSource(char &data);
  
  void bad()
  {
      char data;
      data = ' ';
      badSource(data);
      if(data > 0) /* ensure we won't have an underflow */
      {
          /* POTENTIAL FLAW: if (data*2) > CHAR_MAX, this will overflow */
          char result = data * 2;
          printHexCharLine(result);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSource(char &data);
  
  static void goodG2B()
  {
      char data;
      data = ' ';
      goodG2BSource(data);
      if(data > 0) /* ensure we won't have an underflow */
      {
          /* POTENTIAL FLAW: if (data*2) > CHAR_MAX, this will overflow */
          char result = data * 2;
          printHexCharLine(result);
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void goodB2GSource(char &data);
  
  static void goodB2G()
  {
      char data;
      data = ' ';
      goodB2GSource(data);
      if(data > 0) /* ensure we won't have an underflow */
      {
          /* FIX: Add a check to prevent an overflow from occurring */
          if (data < (CHAR_MAX/2))
          {
              char result = data * 2;
              printHexCharLine(result);
          }
          else
          {
              printLine("data value is too large to perform arithmetic safely.");
          }
      }
  }
  
  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 CWE190_Integer_Overflow__char_fscanf_multiply_62; /* 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
  