text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE126_Buffer_Overread__char_alloca_memcpy_15.c
  Label Definition File: CWE126_Buffer_Overread.stack.label.xml
  Template File: sources-sink-15.tmpl.c
  */
  /*
   * @description
   * CWE: 126 Buffer Over-read
   * BadSource:  Set data pointer to a small buffer
   * GoodSource: Set data pointer to a large buffer
   * Sink: memcpy
   *    BadSink : Copy data to string using memcpy
   * Flow Variant: 15 Control flow: switch(6)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifndef OMITBAD
  
  void CWE126_Buffer_Overread__char_alloca_memcpy_15_bad()
  {
      char * data;
      char * data_badbuf = (char *)ALLOCA(50*sizeof(char));
      char * data_goodbuf = (char *)ALLOCA(100*sizeof(char));
      memset(data_badbuf, 'A', 50-1); /* fill with 'A's */
      data_badbuf[50-1] = '\0'; /* null terminate */
      memset(data_goodbuf, 'A', 100-1); /* fill with 'A's */
      data_goodbuf[100-1] = '\0'; /* null terminate */
      switch(6)
      {
      case 6:
          /* FLAW: Set data pointer to a small buffer */
          data = data_badbuf;
          break;
      default:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Set data pointer to a large buffer */
          data = data_goodbuf;
          break;
      }
      {
          char dest[100];
          memset(dest, 'C', 100-1);
          dest[100-1] = '\0'; /* null terminate */
          /* POTENTIAL FLAW: using memcpy with the length of the dest where data
           * could be smaller than dest causing buffer overread */
          memcpy(dest, data, strlen(dest)*sizeof(char));
          dest[100-1] = '\0';
          printLine(dest);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the switch to switch(5) */
  static void goodG2B1()
  {
      char * data;
      char * data_badbuf = (char *)ALLOCA(50*sizeof(char));
      char * data_goodbuf = (char *)ALLOCA(100*sizeof(char));
      memset(data_badbuf, 'A', 50-1); /* fill with 'A's */
      data_badbuf[50-1] = '\0'; /* null terminate */
      memset(data_goodbuf, 'A', 100-1); /* fill with 'A's */
      data_goodbuf[100-1] = '\0'; /* null terminate */
      switch(5)
      {
      case 6:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FLAW: Set data pointer to a small buffer */
          data = data_badbuf;
          break;
      default:
          /* FIX: Set data pointer to a large buffer */
          data = data_goodbuf;
          break;
      }
      {
          char dest[100];
          memset(dest, 'C', 100-1);
          dest[100-1] = '\0'; /* null terminate */
          /* POTENTIAL FLAW: using memcpy with the length of the dest where data
           * could be smaller than dest causing buffer overread */
          memcpy(dest, data, strlen(dest)*sizeof(char));
          dest[100-1] = '\0';
          printLine(dest);
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the switch */
  static void goodG2B2()
  {
      char * data;
      char * data_badbuf = (char *)ALLOCA(50*sizeof(char));
      char * data_goodbuf = (char *)ALLOCA(100*sizeof(char));
      memset(data_badbuf, 'A', 50-1); /* fill with 'A's */
      data_badbuf[50-1] = '\0'; /* null terminate */
      memset(data_goodbuf, 'A', 100-1); /* fill with 'A's */
      data_goodbuf[100-1] = '\0'; /* null terminate */
      switch(6)
      {
      case 6:
          /* FIX: Set data pointer to a large buffer */
          data = data_goodbuf;
          break;
      default:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FLAW: Set data pointer to a small buffer */
          data = data_badbuf;
          break;
      }
      {
          char dest[100];
          memset(dest, 'C', 100-1);
          dest[100-1] = '\0'; /* null terminate */
          /* POTENTIAL FLAW: using memcpy with the length of the dest where data
           * could be smaller than dest causing buffer overread */
          memcpy(dest, data, strlen(dest)*sizeof(char));
          dest[100-1] = '\0';
          printLine(dest);
      }
  }
  
  void CWE126_Buffer_Overread__char_alloca_memcpy_15_good()
  {
      goodG2B1();
      goodG2B2();
  }
  
  #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__char_alloca_memcpy_15_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE126_Buffer_Overread__char_alloca_memcpy_15_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  