text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE194_Unexpected_Sign_Extension__rand_memmove_17.c
  Label Definition File: CWE194_Unexpected_Sign_Extension.label.xml
  Template File: sources-sink-17.tmpl.c
  */
  /*
   * @description
   * CWE: 194 Unexpected Sign Extension
   * BadSource: rand Set data to result of rand(), which could be negative
   * GoodSource: Positive integer
   * Sink: memmove
   *    BadSink : Copy strings using memmove() with the length of data
   * Flow Variant: 17 Control flow: for loops
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef OMITBAD
  
  void CWE194_Unexpected_Sign_Extension__rand_memmove_17_bad()
  {
      int h,i;
      short data;
      /* Initialize data */
      data = 0;
      for(h = 0; h < 0; h++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use a positive integer less than &InitialDataSize&*/
          data = 100-1;
      }
      for(i = 0; i < 1; i++)
      {
          /* POTENTIAL FLAW: Use a random value that could be less than 0 */
          data = (short)RAND32();
      }
      {
          char src[100];
          char dest[100] = "";
          memset(src, 'A', 100-1);
          src[100-1] = '\0';
          if (data < 100)
          {
              /* POTENTIAL FLAW: data is interpreted as an unsigned int - if its value is negative,
               * the sign extension could result in a very large number */
              memmove(dest, src, data);
              dest[data] = '\0'; /* NULL terminate */
          }
          printLine(dest);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() - use goodsource and badsink by changing the conditions on the for statements */
  static void goodG2B()
  {
      int h,i;
      short data;
      /* Initialize data */
      data = 0;
      for(h = 0; h < 1; h++)
      {
          /* FIX: Use a positive integer less than &InitialDataSize&*/
          data = 100-1;
      }
      for(i = 0; i < 0; i++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* POTENTIAL FLAW: Use a random value that could be less than 0 */
          data = (short)RAND32();
      }
      {
          char src[100];
          char dest[100] = "";
          memset(src, 'A', 100-1);
          src[100-1] = '\0';
          if (data < 100)
          {
              /* POTENTIAL FLAW: data is interpreted as an unsigned int - if its value is negative,
               * the sign extension could result in a very large number */
              memmove(dest, src, data);
              dest[data] = '\0'; /* NULL terminate */
          }
          printLine(dest);
      }
  }
  
  void CWE194_Unexpected_Sign_Extension__rand_memmove_17_good()
  {
      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()...");
      CWE194_Unexpected_Sign_Extension__rand_memmove_17_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE194_Unexpected_Sign_Extension__rand_memmove_17_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  