text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE126_Buffer_Overread__CWE129_fgets_54e.c
  Label Definition File: CWE126_Buffer_Overread__CWE129.label.xml
  Template File: sources-sinks-54e.tmpl.c
  */
  /*
   * @description
   * CWE: 126 Buffer Overread
   * 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: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  #ifndef OMITBAD
  
  void CWE126_Buffer_Overread__CWE129_fgets_54e_badSink(int data)
  {
      {
          int buffer[10] = { 0 };
          /* POTENTIAL FLAW: Attempt to access an index of the array that is above the upper bound
           * This check does not check the upper bounds of the array index */
          if (data >= 0)
          {
              printIntLine(buffer[data]);
          }
          else
          {
              printLine("ERROR: Array index is negative");
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE126_Buffer_Overread__CWE129_fgets_54e_goodG2BSink(int data)
  {
      {
          int buffer[10] = { 0 };
          /* POTENTIAL FLAW: Attempt to access an index of the array that is above the upper bound
           * This check does not check the upper bounds of the array index */
          if (data >= 0)
          {
              printIntLine(buffer[data]);
          }
          else
          {
              printLine("ERROR: Array index is negative");
          }
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE126_Buffer_Overread__CWE129_fgets_54e_goodB2GSink(int data)
  {
      {
          int buffer[10] = { 0 };
          /* FIX: Properly validate the array index and prevent a buffer overread */
          if (data >= 0 && data < (10))
          {
              printIntLine(buffer[data]);
          }
          else
          {
              printLine("ERROR: Array index is out-of-bounds");
          }
      }
  }
  
  #endif /* OMITGOOD */
  