text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE806_char_ncpy_84_bad.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE806.label.xml
  Template File: sources-sink-84_bad.tmpl.cpp
  */
  /*
   * @description
   * CWE: 122 Heap Based Buffer Overflow
   * BadSource:  Initialize data as a large string
   * GoodSource: Initialize data as a small string
   * Sinks: ncpy
   *    BadSink : Copy data to string using strncpy
   * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use
   *
   * */
  #ifndef OMITBAD
  
  #include "std_testcase.h"
  #include "CWE122_Heap_Based_Buffer_Overflow__c_CWE806_char_ncpy_84.h"
  
  namespace CWE122_Heap_Based_Buffer_Overflow__c_CWE806_char_ncpy_84
  {
  CWE122_Heap_Based_Buffer_Overflow__c_CWE806_char_ncpy_84_bad::CWE122_Heap_Based_Buffer_Overflow__c_CWE806_char_ncpy_84_bad(char * dataCopy)
  {
      data = dataCopy;
      /* FLAW: Initialize data as a large buffer that is larger than the small buffer used in the sink */
      memset(data, 'A', 100-1); /* fill with 'A's */
      data[100-1] = '\0'; /* null terminate */
  }
  
  CWE122_Heap_Based_Buffer_Overflow__c_CWE806_char_ncpy_84_bad::~CWE122_Heap_Based_Buffer_Overflow__c_CWE806_char_ncpy_84_bad()
  {
      {
          char dest[50] = "";
          /* POTENTIAL FLAW: Possible buffer overflow if data is larger than dest */
          strncpy(dest, data, strlen(data));
          dest[50-1] = '\0'; /* Ensure the destination buffer is null terminated */
          printLine(data);
          free(data);
      }
  }
  }
  #endif /* OMITBAD */
  