text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE191_Integer_Underflow__short_rand_multiply_53d.c
  Label Definition File: CWE191_Integer_Underflow.label.xml
  Template File: sources-sinks-53d.tmpl.c
  */
  /*
   * @description
   * CWE: 191 Integer Underflow
   * BadSource: rand Set data to result of rand()
   * GoodSource: Set data to a small, non-zero number (negative two)
   * Sinks: multiply
   *    GoodSink: Ensure there will not be an underflow before multiplying data by 2
   *    BadSink : If data is negative, multiply by 2, which can cause an underflow
   * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef OMITBAD
  
  void CWE191_Integer_Underflow__short_rand_multiply_53d_badSink(short data)
  {
      if(data < 0) /* ensure we won't have an overflow */
      {
          /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */
          short result = data * 2;
          printIntLine(result);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE191_Integer_Underflow__short_rand_multiply_53d_goodG2BSink(short data)
  {
      if(data < 0) /* ensure we won't have an overflow */
      {
          /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */
          short result = data * 2;
          printIntLine(result);
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE191_Integer_Underflow__short_rand_multiply_53d_goodB2GSink(short data)
  {
      if(data < 0) /* ensure we won't have an overflow */
      {
          /* FIX: Add a check to prevent an underflow from occurring */
          if (data > (SHRT_MIN/2))
          {
              short result = data * 2;
              printIntLine(result);
          }
          else
          {
              printLine("data value is too small to perform multiplication.");
          }
      }
  }
  
  #endif /* OMITGOOD */
  