text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129_rand_64b.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129.label.xml
  Template File: sources-sinks-64b.tmpl.cpp
  */
  /*
   * @description
   * CWE: 122 Heap Based Buffer Overflow
   * BadSource: rand Set data to result of rand(), which may be zero
   * GoodSource: Larger than zero but less than 10
   * Sinks:
   *    GoodSink: Ensure the array index is valid
   *    BadSink : Improperly check the array index by not checking the upper bound
   * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE129_rand_64
  {
  
  #ifndef OMITBAD
  
  void badSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      int * dataPtr = (int *)dataVoidPtr;
      /* dereference dataPtr into data */
      int data = (*dataPtr);
      {
          int i;
          int * buffer = new int[10];
          /* initialize buffer */
          for (i = 0; i < 10; i++)
          {
              buffer[i] = 0;
          }
          /* POTENTIAL FLAW: Attempt to write to an index of the array that is above the upper bound
          * This code does check to see if the array index is negative */
          if (data >= 0)
          {
              buffer[data] = 1;
              /* Print the array values */
              for(i = 0; i < 10; i++)
              {
                  printIntLine(buffer[i]);
              }
          }
          else
          {
              printLine("ERROR: Array index is negative.");
          }
          delete[] buffer;
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      int * dataPtr = (int *)dataVoidPtr;
      /* dereference dataPtr into data */
      int data = (*dataPtr);
      {
          int i;
          int * buffer = new int[10];
          /* initialize buffer */
          for (i = 0; i < 10; i++)
          {
              buffer[i] = 0;
          }
          /* POTENTIAL FLAW: Attempt to write to an index of the array that is above the upper bound
          * This code does check to see if the array index is negative */
          if (data >= 0)
          {
              buffer[data] = 1;
              /* Print the array values */
              for(i = 0; i < 10; i++)
              {
                  printIntLine(buffer[i]);
              }
          }
          else
          {
              printLine("ERROR: Array index is negative.");
          }
          delete[] buffer;
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void goodB2GSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      int * dataPtr = (int *)dataVoidPtr;
      /* dereference dataPtr into data */
      int data = (*dataPtr);
      {
          int i;
          int * buffer = new int[10];
          /* initialize buffer */
          for (i = 0; i < 10; i++)
          {
              buffer[i] = 0;
          }
          /* FIX: Properly validate the array index and prevent a buffer overflow */
          if (data >= 0 && data < (10))
          {
              buffer[data] = 1;
              /* Print the array values */
              for(i = 0; i < 10; i++)
              {
                  printIntLine(buffer[i]);
              }
          }
          else
          {
              printLine("ERROR: Array index is out-of-bounds");
          }
          delete[] buffer;
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  