text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE126_Buffer_Overread__char_alloca_loop_44.c
  Label Definition File: CWE126_Buffer_Overread.stack.label.xml
  Template File: sources-sink-44.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: 44 Data/control flow: data passed as an argument from one function to a function in the same source file called via a function pointer
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifndef OMITBAD
  
  static void badSink(char * data)
  {
      {
          size_t i, destLen;
          char dest[100];
          memset(dest, 'C', 100-1);
          dest[100-1] = '\0'; /* null terminate */
          destLen = strlen(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] = '\0';
          printLine(dest);
      }
  }
  
  void CWE126_Buffer_Overread__char_alloca_loop_44_bad()
  {
      char * data;
      /* define a function pointer */
      void (*funcPtr) (char *) = badSink;
      char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));
      char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));
      memset(dataBadBuffer, 'A', 50-1); /* fill with 'A's */
      dataBadBuffer[50-1] = '\0'; /* null terminate */
      memset(dataGoodBuffer, 'A', 100-1); /* fill with 'A's */
      dataGoodBuffer[100-1] = '\0'; /* null terminate */
      /* FLAW: Set data pointer to a small buffer */
      data = dataBadBuffer;
      /* use the function pointer */
      funcPtr(data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  static void goodG2BSink(char * data)
  {
      {
          size_t i, destLen;
          char dest[100];
          memset(dest, 'C', 100-1);
          dest[100-1] = '\0'; /* null terminate */
          destLen = strlen(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] = '\0';
          printLine(dest);
      }
  }
  
  static void goodG2B()
  {
      char * data;
      void (*funcPtr) (char *) = goodG2BSink;
      char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));
      char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));
      memset(dataBadBuffer, 'A', 50-1); /* fill with 'A's */
      dataBadBuffer[50-1] = '\0'; /* null terminate */
      memset(dataGoodBuffer, 'A', 100-1); /* fill with 'A's */
      dataGoodBuffer[100-1] = '\0'; /* null terminate */
      /* FIX: Set data pointer to a large buffer */
      data = dataGoodBuffer;
      funcPtr(data);
  }
  
  void CWE126_Buffer_Overread__char_alloca_loop_44_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__char_alloca_loop_44_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE126_Buffer_Overread__char_alloca_loop_44_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  