text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE195_Signed_To_Unsigned_Conversion__rand_strncpy_14.c
  Label Definition File: CWE195_Signed_To_Unsigned_Conversion.label.xml
  Template File: sources-sink-14.tmpl.c
  */
  /*
   * @description
   * CWE: 195 Signed to Unsigned Conversion
   * BadSource: rand Set data to result of rand(), which may be zero
   * GoodSource: Positive integer
   * Sink: strncpy
   *    BadSink : Copy strings using strncpy() with the length of data
   * Flow Variant: 14 Control flow: if(global_five==5) and if(global_five!=5)
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef OMITBAD
  
  void CWE195_Signed_To_Unsigned_Conversion__rand_strncpy_14_bad()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(global_five==5)
      {
          data = RAND32();
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use a positive integer less than &InitialDataSize&*/
          data = 100-1;
      }
      {
          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 conversion could result in a very large number */
              strncpy(dest, src, data);
              dest[data] = '\0'; /* strncpy() does not always NULL terminate */
          }
          printLine(dest);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the global_five==5 to global_five!=5 */
  static void goodG2B1()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(global_five!=5)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          data = RAND32();
      }
      else
      {
          /* FIX: Use a positive integer less than &InitialDataSize&*/
          data = 100-1;
      }
      {
          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 conversion could result in a very large number */
              strncpy(dest, src, data);
              dest[data] = '\0'; /* strncpy() does not always NULL terminate */
          }
          printLine(dest);
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(global_five==5)
      {
          /* FIX: Use a positive integer less than &InitialDataSize&*/
          data = 100-1;
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          data = 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 conversion could result in a very large number */
              strncpy(dest, src, data);
              dest[data] = '\0'; /* strncpy() does not always NULL terminate */
          }
          printLine(dest);
      }
  }
  
  void CWE195_Signed_To_Unsigned_Conversion__rand_strncpy_14_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()...");
      CWE195_Signed_To_Unsigned_Conversion__rand_strncpy_14_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE195_Signed_To_Unsigned_Conversion__rand_strncpy_14_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  