text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE127_Buffer_Underread__CWE839_fgets_42.c
  Label Definition File: CWE127_Buffer_Underread__CWE839.label.xml
  Template File: sources-sinks-42.tmpl.c
  */
  /*
   * @description
   * CWE: 127 Buffer Underread
   * BadSource: fgets Read data from the console using fgets()
   * GoodSource: Non-negative but less than 10
   * Sinks:
   *    GoodSink: Ensure the array index is valid
   *    BadSink : Improperly check the array index by not checking to see if the value is negative
   * Flow Variant: 42 Data flow: data returned from one function to another in the same source file
   *
   * */
  
  #include "std_testcase.h"
  
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  #ifndef OMITBAD
  
  static int badSource(int data)
  {
      {
          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.");
          }
      }
      return data;
  }
  
  void CWE127_Buffer_Underread__CWE839_fgets_42_bad()
  {
      int data;
      /* Initialize data */
      data = -1;
      data = badSource(data);
      {
          int buffer[10] = { 0 };
          /* POTENTIAL FLAW: Attempt to access a negative index of the array
           * This check does not check to see if the array index is negative */
          if (data < 10)
          {
              printIntLine(buffer[data]);
          }
          else
          {
              printLine("ERROR: Array index is too big.");
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  static int goodG2BSource(int data)
  {
      /* FIX: Use a value greater than 0, but less than 10 to avoid attempting to
       * access an index of the array in the sink that is out-of-bounds */
      data = 7;
      return data;
  }
  
  static void goodG2B()
  {
      int data;
      /* Initialize data */
      data = -1;
      data = goodG2BSource(data);
      {
          int buffer[10] = { 0 };
          /* POTENTIAL FLAW: Attempt to access a negative index of the array
           * This check does not check to see if the array index is negative */
          if (data < 10)
          {
              printIntLine(buffer[data]);
          }
          else
          {
              printLine("ERROR: Array index is too big.");
          }
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  static int goodB2GSource(int data)
  {
      {
          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.");
          }
      }
      return data;
  }
  
  static void goodB2G()
  {
      int data;
      /* Initialize data */
      data = -1;
      data = goodB2GSource(data);
      {
          int buffer[10] = { 0 };
          /* FIX: Properly validate the array index and prevent a buffer underread */
          if (data >= 0 && data < (10))
          {
              printIntLine(buffer[data]);
          }
          else
          {
              printLine("ERROR: Array index is out-of-bounds");
          }
      }
  }
  
  void CWE127_Buffer_Underread__CWE839_fgets_42_good()
  {
      goodB2G();
      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()...");
      CWE127_Buffer_Underread__CWE839_fgets_42_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE127_Buffer_Underread__CWE839_fgets_42_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  