text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE124_Buffer_Underwrite__char_alloca_loop_32.c
  Label Definition File: CWE124_Buffer_Underwrite.stack.label.xml
  Template File: sources-sink-32.tmpl.c
  */
  /*
   * @description
   * CWE: 124 Buffer Underwrite
   * BadSource:  Set data pointer to before the allocated memory buffer
   * GoodSource: Set data pointer to the allocated memory buffer
   * Sink: loop
   *    BadSink : Copy string to data using a loop
   * Flow Variant: 32 Data flow using two pointers to the same value within the same function
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifndef OMITBAD
  
  void CWE124_Buffer_Underwrite__char_alloca_loop_32_bad()
  {
      char * data;
      char * *dataPtr1 = &data;
      char * *dataPtr2 = &data;
      char * dataBuffer = (char *)ALLOCA(100*sizeof(char));
      memset(dataBuffer, 'A', 100-1);
      dataBuffer[100-1] = '\0';
      {
          char * data = *dataPtr1;
          /* FLAW: Set data pointer to before the allocated memory buffer */
          data = dataBuffer - 8;
          *dataPtr1 = data;
      }
      {
          char * data = *dataPtr2;
          {
              size_t i;
              char source[100];
              memset(source, 'C', 100-1); /* fill with 'C's */
              source[100-1] = '\0'; /* null terminate */
              /* POTENTIAL FLAW: Possibly copying data to memory before the destination buffer */
              for (i = 0; i < 100; i++)
              {
                  data[i] = source[i];
              }
              /* Ensure the destination buffer is null terminated */
              data[100-1] = '\0';
              printLine(data);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  static void goodG2B()
  {
      char * data;
      char * *dataPtr1 = &data;
      char * *dataPtr2 = &data;
      char * dataBuffer = (char *)ALLOCA(100*sizeof(char));
      memset(dataBuffer, 'A', 100-1);
      dataBuffer[100-1] = '\0';
      {
          char * data = *dataPtr1;
          /* FIX: Set data pointer to the allocated memory buffer */
          data = dataBuffer;
          *dataPtr1 = data;
      }
      {
          char * data = *dataPtr2;
          {
              size_t i;
              char source[100];
              memset(source, 'C', 100-1); /* fill with 'C's */
              source[100-1] = '\0'; /* null terminate */
              /* POTENTIAL FLAW: Possibly copying data to memory before the destination buffer */
              for (i = 0; i < 100; i++)
              {
                  data[i] = source[i];
              }
              /* Ensure the destination buffer is null terminated */
              data[100-1] = '\0';
              printLine(data);
          }
      }
  }
  
  void CWE124_Buffer_Underwrite__char_alloca_loop_32_good()
  {
      goodG2B();
  }
  
  #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()...");
      CWE124_Buffer_Underwrite__char_alloca_loop_32_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE124_Buffer_Underwrite__char_alloca_loop_32_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  