text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE126_Buffer_Overread__wchar_t_alloca_loop_34.c
  Label Definition File: CWE126_Buffer_Overread.stack.label.xml
  Template File: sources-sink-34.tmpl.c
  */
  /*
   * @description
   * CWE: 126 Buffer Over-read
   * BadSource:  Set data pointer to a small buffer
   * GoodSource: Set data pointer to a large buffer
   * Sinks: loop
   *    BadSink : Copy data to string using a loop
   * Flow Variant: 34 Data flow: use of a union containing two methods of accessing the same data (within the same function)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  typedef union
  {
      wchar_t * unionFirst;
      wchar_t * unionSecond;
  } CWE126_Buffer_Overread__wchar_t_alloca_loop_34_unionType;
  
  #ifndef OMITBAD
  
  void CWE126_Buffer_Overread__wchar_t_alloca_loop_34_bad()
  {
      wchar_t * data;
      CWE126_Buffer_Overread__wchar_t_alloca_loop_34_unionType myUnion;
      wchar_t * dataBadBuffer = (wchar_t *)ALLOCA(50*sizeof(wchar_t));
      wchar_t * dataGoodBuffer = (wchar_t *)ALLOCA(100*sizeof(wchar_t));
      wmemset(dataBadBuffer, L'A', 50-1); /* fill with 'A's */
      dataBadBuffer[50-1] = L'\0'; /* null terminate */
      wmemset(dataGoodBuffer, L'A', 100-1); /* fill with 'A's */
      dataGoodBuffer[100-1] = L'\0'; /* null terminate */
      /* FLAW: Set data pointer to a small buffer */
      data = dataBadBuffer;
      myUnion.unionFirst = data;
      {
          wchar_t * data = myUnion.unionSecond;
          {
              size_t i, destLen;
              wchar_t dest[100];
              wmemset(dest, L'C', 100-1);
              dest[100-1] = L'\0'; /* null terminate */
              destLen = wcslen(dest);
              /* POTENTIAL FLAW: using length of the dest where data
               * could be smaller than dest causing buffer overread */
              for (i = 0; i < destLen; i++)
              {
                  dest[i] = data[i];
              }
              dest[100-1] = L'\0';
              printWLine(dest);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  static void goodG2B()
  {
      wchar_t * data;
      CWE126_Buffer_Overread__wchar_t_alloca_loop_34_unionType myUnion;
      wchar_t * dataBadBuffer = (wchar_t *)ALLOCA(50*sizeof(wchar_t));
      wchar_t * dataGoodBuffer = (wchar_t *)ALLOCA(100*sizeof(wchar_t));
      wmemset(dataBadBuffer, L'A', 50-1); /* fill with 'A's */
      dataBadBuffer[50-1] = L'\0'; /* null terminate */
      wmemset(dataGoodBuffer, L'A', 100-1); /* fill with 'A's */
      dataGoodBuffer[100-1] = L'\0'; /* null terminate */
      /* FIX: Set data pointer to a large buffer */
      data = dataGoodBuffer;
      myUnion.unionFirst = data;
      {
          wchar_t * data = myUnion.unionSecond;
          {
              size_t i, destLen;
              wchar_t dest[100];
              wmemset(dest, L'C', 100-1);
              dest[100-1] = L'\0'; /* null terminate */
              destLen = wcslen(dest);
              /* POTENTIAL FLAW: using length of the dest where data
               * could be smaller than dest causing buffer overread */
              for (i = 0; i < destLen; i++)
              {
                  dest[i] = data[i];
              }
              dest[100-1] = L'\0';
              printWLine(dest);
          }
      }
  }
  
  void CWE126_Buffer_Overread__wchar_t_alloca_loop_34_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()...");
      CWE126_Buffer_Overread__wchar_t_alloca_loop_34_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE126_Buffer_Overread__wchar_t_alloca_loop_34_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  