text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE190_Integer_Overflow__unsigned_int_rand_multiply_01.c
  Label Definition File: CWE190_Integer_Overflow.label.xml
  Template File: sources-sinks-01.tmpl.c
  */
  /*
   * @description
   * CWE: 190 Integer Overflow
   * BadSource: rand Set data to result of rand()
   * 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: 01 Baseline
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef OMITBAD
  
  void CWE190_Integer_Overflow__unsigned_int_rand_multiply_01_bad()
  {
      unsigned int data;
      data = 0;
      /* POTENTIAL FLAW: Use a random value */
      data = (unsigned int)RAND32();
      if(data > 0) /* ensure we won't have an underflow */
      {
          /* POTENTIAL FLAW: if (data*2) > UINT_MAX, this will overflow */
          unsigned int result = data * 2;
          printUnsignedLine(result);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  static void goodG2B()
  {
      unsigned int data;
      data = 0;
      /* FIX: Use a small, non-zero value that will not cause an overflow in the sinks */
      data = 2;
      if(data > 0) /* ensure we won't have an underflow */
      {
          /* POTENTIAL FLAW: if (data*2) > UINT_MAX, this will overflow */
          unsigned int result = data * 2;
          printUnsignedLine(result);
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  static void goodB2G()
  {
      unsigned int data;
      data = 0;
      /* POTENTIAL FLAW: Use a random value */
      data = (unsigned int)RAND32();
      if(data > 0) /* ensure we won't have an underflow */
      {
          /* FIX: Add a check to prevent an overflow from occurring */
          if (data < (UINT_MAX/2))
          {
              unsigned int result = data * 2;
              printUnsignedLine(result);
          }
          else
          {
              printLine("data value is too large to perform arithmetic safely.");
          }
      }
  }
  
  void CWE190_Integer_Overflow__unsigned_int_rand_multiply_01_good()
  {
      goodG2B();
      goodB2G();
  }
  
  #endif /* OMITGOOD */
  
  /* 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
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      CWE190_Integer_Overflow__unsigned_int_rand_multiply_01_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE190_Integer_Overflow__unsigned_int_rand_multiply_01_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  