text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE121_Stack_Based_Buffer_Overflow__CWE129_fgets_83_bad.cpp
  Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE129.label.xml
  Template File: sources-sinks-83_bad.tmpl.cpp
  */
  /*
   * @description
   * CWE: 121 Stack Based Buffer Overflow
   * BadSource: fgets Read data from the console using fgets()
   * GoodSource: Larger than zero but less than 10
   * Sinks:
   *    GoodSink: Ensure the array index is valid
   *    BadSink : Improperly check the array index by not checking the upper bound
   * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack
   *
   * */
  #ifndef OMITBAD
  
  #include "std_testcase.h"
  #include "CWE121_Stack_Based_Buffer_Overflow__CWE129_fgets_83.h"
  
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  namespace CWE121_Stack_Based_Buffer_Overflow__CWE129_fgets_83
  {
  CWE121_Stack_Based_Buffer_Overflow__CWE129_fgets_83_bad::CWE121_Stack_Based_Buffer_Overflow__CWE129_fgets_83_bad(int dataCopy)
  {
      data = dataCopy;
      {
          char inputBuffer[CHAR_ARRAY_SIZE] = "";
          /* POTENTIAL FLAW: Read data from the console using fgets() */
          if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)
          {
              /* Convert to int */
              data = atoi(inputBuffer);
          }
          else
          {
              printLine("fgets() failed.");
          }
      }
  }
  
  CWE121_Stack_Based_Buffer_Overflow__CWE129_fgets_83_bad::~CWE121_Stack_Based_Buffer_Overflow__CWE129_fgets_83_bad()
  {
      {
          int i;
          int buffer[10] = { 0 };
          /* POTENTIAL FLAW: Attempt to write to an index of the array that is above the upper bound
          * This code does check to see if the array index is negative */
          if (data >= 0)
          {
              buffer[data] = 1;
              /* Print the array values */
              for(i = 0; i < 10; i++)
              {
                  printIntLine(buffer[i]);
              }
          }
          else
          {
              printLine("ERROR: Array index is negative.");
          }
      }
  }
  }
  #endif /* OMITBAD */
  