text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE191_Integer_Underflow__short_rand_sub_43.cpp
  Label Definition File: CWE191_Integer_Underflow.label.xml
  Template File: sources-sinks-43.tmpl.cpp
  */
  /*
   * @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: sub
   *    GoodSink: Ensure there will not be an underflow before subtracting 1 from data
   *    BadSink : Subtract 1 from data, which can cause an Underflow
   * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file
   *
   * */
  
  #include "std_testcase.h"
  
  namespace CWE191_Integer_Underflow__short_rand_sub_43
  {
  
  #ifndef OMITBAD
  
  static void badSource(short &data)
  {
      /* POTENTIAL FLAW: Use a random value */
      data = (short)RAND32();
  }
  
  void bad()
  {
      short data;
      data = 0;
      badSource(data);
      {
          /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */
          short result = data - 1;
          printIntLine(result);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  static void goodG2BSource(short &data)
  {
      /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */
      data = -2;
  }
  
  static void goodG2B()
  {
      short data;
      data = 0;
      goodG2BSource(data);
      {
          /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */
          short result = data - 1;
          printIntLine(result);
      }
  }
  
  /* goodB2G() uses the BadSource with the GoodSink */
  static void goodB2GSource(short &data)
  {
      /* POTENTIAL FLAW: Use a random value */
      data = (short)RAND32();
  }
  
  static void goodB2G()
  {
      short data;
      data = 0;
      goodB2GSource(data);
      /* FIX: Add a check to prevent an underflow from occurring */
      if (data > SHRT_MIN)
      {
          short result = data - 1;
          printIntLine(result);
      }
      else
      {
          printLine("data value is too large to perform subtraction.");
      }
  }
  
  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 CWE191_Integer_Underflow__short_rand_sub_43; /* 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
  