text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE121_Stack_Based_Buffer_Overflow__placement_new_declare_11.cpp
  Label Definition File: CWE121_Stack_Based_Buffer_Overflow__placement_new.label.xml
  Template File: sources-sinks-11.tmpl.cpp
  */
  /*
   * @description
   * CWE: 121 Stack Based Buffer Overflow
   * BadSource:  Initialize data to a small buffer
   * GoodSource: Initialize data to a buffer large enough to hold a TwoIntsClass
   * Sinks:
   *    GoodSink: Allocate a new class using placement new and a buffer that is large enough to hold the class
   *    BadSink : Allocate a new class using placement new and a buffer that is too small
   * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())
   * */
  
  #include "std_testcase.h"
  
  namespace CWE121_Stack_Based_Buffer_Overflow__placement_new_declare_11
  {
  
  #ifndef OMITBAD
  
  void bad()
  {
      char * data;
      char dataBadBuffer[sizeof(OneIntClass)];
      char dataGoodBuffer[sizeof(TwoIntsClass)];
      if(globalReturnsTrue())
      {
          /* POTENTIAL FLAW: Initialize data to a buffer smaller than the sizeof(TwoIntsClass) */
          data = dataBadBuffer;
      }
      if(globalReturnsTrue())
      {
          {
              /* The Visual C++ compiler generates a warning if you initialize the class with ().
               * This will cause the compile to default-initialize the object.
               * See http://msdn.microsoft.com/en-us/library/wewb47ee%28v=VS.100%29.aspx
               */
              /* POTENTIAL FLAW: data may not be large enough to hold a TwoIntsClass */
              TwoIntsClass * classTwo = new(data) TwoIntsClass;
              /* Initialize and make use of the class */
              classTwo->intOne = 5;
              classTwo->intTwo = 10; /* POTENTIAL FLAW: If sizeof(data) < sizeof(TwoIntsClass) then this line will be a buffer overflow */
              printIntLine(classTwo->intOne);
              /* skip printing classTwo->intTwo since that could be a buffer overread */
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */
  static void goodB2G1()
  {
      char * data;
      char dataBadBuffer[sizeof(OneIntClass)];
      char dataGoodBuffer[sizeof(TwoIntsClass)];
      if(globalReturnsTrue())
      {
          /* POTENTIAL FLAW: Initialize data to a buffer smaller than the sizeof(TwoIntsClass) */
          data = dataBadBuffer;
      }
      if(globalReturnsFalse())
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          {
              /* The Visual C++ compiler generates a warning if you initialize the class with ().
               * This will cause the compile to default-initialize the object.
               * See http://msdn.microsoft.com/en-us/library/wewb47ee%28v=VS.100%29.aspx
               */
              /* FIX: data will at least be the sizeof(OneIntClass) */
              OneIntClass * classOne = new(data) OneIntClass;
              /* Initialize and make use of the class */
              classOne->intOne = 5;
              printIntLine(classOne->intOne);
          }
      }
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  static void goodB2G2()
  {
      char * data;
      char dataBadBuffer[sizeof(OneIntClass)];
      char dataGoodBuffer[sizeof(TwoIntsClass)];
      if(globalReturnsTrue())
      {
          /* POTENTIAL FLAW: Initialize data to a buffer smaller than the sizeof(TwoIntsClass) */
          data = dataBadBuffer;
      }
      if(globalReturnsTrue())
      {
          {
              /* The Visual C++ compiler generates a warning if you initialize the class with ().
               * This will cause the compile to default-initialize the object.
               * See http://msdn.microsoft.com/en-us/library/wewb47ee%28v=VS.100%29.aspx
               */
              /* FIX: data will at least be the sizeof(OneIntClass) */
              OneIntClass * classOne = new(data) OneIntClass;
              /* Initialize and make use of the class */
              classOne->intOne = 5;
              printIntLine(classOne->intOne);
          }
      }
  }
  
  /* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */
  static void goodG2B1()
  {
      char * data;
      char dataBadBuffer[sizeof(OneIntClass)];
      char dataGoodBuffer[sizeof(TwoIntsClass)];
      if(globalReturnsFalse())
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: Initialize to a buffer at least the sizeof(TwoIntsClass) */
          data = dataGoodBuffer;
      }
      if(globalReturnsTrue())
      {
          {
              /* The Visual C++ compiler generates a warning if you initialize the class with ().
               * This will cause the compile to default-initialize the object.
               * See http://msdn.microsoft.com/en-us/library/wewb47ee%28v=VS.100%29.aspx
               */
              /* POTENTIAL FLAW: data may not be large enough to hold a TwoIntsClass */
              TwoIntsClass * classTwo = new(data) TwoIntsClass;
              /* Initialize and make use of the class */
              classTwo->intOne = 5;
              classTwo->intTwo = 10; /* POTENTIAL FLAW: If sizeof(data) < sizeof(TwoIntsClass) then this line will be a buffer overflow */
              printIntLine(classTwo->intOne);
              /* skip printing classTwo->intTwo since that could be a buffer overread */
          }
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  static void goodG2B2()
  {
      char * data;
      char dataBadBuffer[sizeof(OneIntClass)];
      char dataGoodBuffer[sizeof(TwoIntsClass)];
      if(globalReturnsTrue())
      {
          /* FIX: Initialize to a buffer at least the sizeof(TwoIntsClass) */
          data = dataGoodBuffer;
      }
      if(globalReturnsTrue())
      {
          {
              /* The Visual C++ compiler generates a warning if you initialize the class with ().
               * This will cause the compile to default-initialize the object.
               * See http://msdn.microsoft.com/en-us/library/wewb47ee%28v=VS.100%29.aspx
               */
              /* POTENTIAL FLAW: data may not be large enough to hold a TwoIntsClass */
              TwoIntsClass * classTwo = new(data) TwoIntsClass;
              /* Initialize and make use of the class */
              classTwo->intOne = 5;
              classTwo->intTwo = 10; /* POTENTIAL FLAW: If sizeof(data) < sizeof(TwoIntsClass) then this line will be a buffer overflow */
              printIntLine(classTwo->intOne);
              /* skip printing classTwo->intTwo since that could be a buffer overread */
          }
      }
  }
  
  void good()
  {
      goodB2G1();
      goodB2G2();
      goodG2B1();
      goodG2B2();
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  
  /* 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
  
  using namespace CWE121_Stack_Based_Buffer_Overflow__placement_new_declare_11; /* so that we can use good and bad easily */
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  