text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE126_Buffer_Overread__CWE129_rand_16.c
  Label Definition File: CWE126_Buffer_Overread__CWE129.label.xml
  Template File: sources-sinks-16.tmpl.c
  */
  /*
   * @description
   * CWE: 126 Buffer Overread
   * BadSource: rand Set data to result of rand(), which may be zero
   * 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: 16 Control flow: while(1)
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef OMITBAD
  
  void CWE126_Buffer_Overread__CWE129_rand_16_bad()
  {
      int data;
      /* Initialize data */
      data = -1;
      while(1)
      {
          /* POTENTIAL FLAW: Set data to a random value */
          data = RAND32();
          break;
      }
      while(1)
      {
          {
              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");
              }
          }
          break;
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */
  static void goodB2G()
  {
      int data;
      /* Initialize data */
      data = -1;
      while(1)
      {
          /* POTENTIAL FLAW: Set data to a random value */
          data = RAND32();
          break;
      }
      while(1)
      {
          {
              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");
              }
          }
          break;
      }
  }
  
  /* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */
  static void goodG2B()
  {
      int data;
      /* Initialize data */
      data = -1;
      while(1)
      {
          /* 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;
          break;
      }
      while(1)
      {
          {
              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");
              }
          }
          break;
      }
  }
  
  void CWE126_Buffer_Overread__CWE129_rand_16_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()...");
      CWE126_Buffer_Overread__CWE129_rand_16_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE126_Buffer_Overread__CWE129_rand_16_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  