text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE197_Numeric_Truncation_Error__short_rand_13.c
  Label Definition File: CWE197_Numeric_Truncation_Error__short.label.xml
  Template File: sources-sink-13.tmpl.c
  */
  /*
   * @description
   * CWE: 197 Numeric Truncation Error
   * BadSource: rand Set data to result of rand(), which could be negative
   * GoodSource: Less than CHAR_MAX
   * Sink:
   *    BadSink : Convert data to a char
   * Flow Variant: 13 Control flow: if(global_const_five==5) and if(global_const_five!=5)
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef OMITBAD
  
  void CWE197_Numeric_Truncation_Error__short_rand_13_bad()
  {
      short data;
      /* Initialize data */
      data = -1;
      if(global_const_five==5)
      {
          /* POTENTIAL FLAW: Use a random number */
          data = (short)RAND32();
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use a positive integer less than CHAR_MAX*/
          data = CHAR_MAX-5;
      }
      {
          /* POTENTIAL FLAW: Convert data to a char, possibly causing a truncation error */
          char c = (char)data;
          printHexCharLine(c);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the global_const_five==5 to global_const_five!=5 */
  static void goodG2B1()
  {
      short data;
      /* Initialize data */
      data = -1;
      if(global_const_five!=5)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* POTENTIAL FLAW: Use a random number */
          data = (short)RAND32();
      }
      else
      {
          /* FIX: Use a positive integer less than CHAR_MAX*/
          data = CHAR_MAX-5;
      }
      {
          /* POTENTIAL FLAW: Convert data to a char, possibly causing a truncation error */
          char c = (char)data;
          printHexCharLine(c);
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      short data;
      /* Initialize data */
      data = -1;
      if(global_const_five==5)
      {
          /* FIX: Use a positive integer less than CHAR_MAX*/
          data = CHAR_MAX-5;
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* POTENTIAL FLAW: Use a random number */
          data = (short)RAND32();
      }
      {
          /* POTENTIAL FLAW: Convert data to a char, possibly causing a truncation error */
          char c = (char)data;
          printHexCharLine(c);
      }
  }
  
  void CWE197_Numeric_Truncation_Error__short_rand_13_good()
  {
      goodG2B1();
      goodG2B2();
  }
  
  #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()...");
      CWE197_Numeric_Truncation_Error__short_rand_13_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE197_Numeric_Truncation_Error__short_rand_13_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  