text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE400_Resource_Exhaustion__rand_sleep_42.c
  Label Definition File: CWE400_Resource_Exhaustion.label.xml
  Template File: sources-sinks-42.tmpl.c
  */
  /*
   * @description
   * CWE: 400 Resource Exhaustion
   * BadSource: rand Set data to result of rand(), which may be zero
   * GoodSource: Assign count to be a relatively small number
   * Sinks: sleep
   *    GoodSink: Validate count before using it as a parameter in sleep function
   *    BadSink : Use count as the parameter for sleep withhout checking it's size first
   * Flow Variant: 42 Data flow: data returned from one function to another in the same source file
   *
   * */
  
  #include "std_testcase.h"
  
  #ifdef _WIN32
  #include <windows.h>
  #define SLEEP Sleep
  #else
  #include <unistd.h>
  #define SLEEP usleep
  #endif
  
  #ifndef OMITBAD
  
  static int badSource(int count)
  {
      /* POTENTIAL FLAW: Set count to a random value */
      count = RAND32();
      return count;
  }
  
  void CWE400_Resource_Exhaustion__rand_sleep_42_bad()
  {
      int count;
      /* Initialize count */
      count = -1;
      count = badSource(count);
      /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */
      SLEEP(count);
      printLine("Sleep time possibly too long");
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  static int goodG2BSource(int count)
  {
      /* FIX: Use a relatively small number */
      count = 20;
      return count;
  }
  
  static void goodG2B()
  {
      int count;
      /* Initialize count */
      count = -1;
      count = goodG2BSource(count);
      /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */
      SLEEP(count);
      printLine("Sleep time possibly too long");
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  static int goodB2GSource(int count)
  {
      /* POTENTIAL FLAW: Set count to a random value */
      count = RAND32();
      return count;
  }
  
  static void goodB2G()
  {
      int count;
      /* Initialize count */
      count = -1;
      count = goodB2GSource(count);
      /* FIX: Validate count before using it as a parameter in the sleep function */
      if (count > 0 && count <= 2000)
      {
          SLEEP(count);
          printLine("Sleep time OK");
      }
      else
      {
          printLine("Sleep time too long");
      }
  }
  
  void CWE400_Resource_Exhaustion__rand_sleep_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()...");
      CWE400_Resource_Exhaustion__rand_sleep_42_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE400_Resource_Exhaustion__rand_sleep_42_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  