text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE194_Unexpected_Sign_Extension__negative_memcpy_51b.c
  Label Definition File: CWE194_Unexpected_Sign_Extension.label.xml
  Template File: sources-sink-51b.tmpl.c
  */
  /*
   * @description
   * CWE: 194 Unexpected Sign Extension
   * BadSource: negative Set data to a fixed negative number
   * GoodSource: Positive integer
   * Sink: memcpy
   *    BadSink : Copy strings using memcpy() with the length of data
   * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */
  
  #ifndef OMITBAD
  
  void CWE194_Unexpected_Sign_Extension__negative_memcpy_51b_bad_sink(short data)
  {
      {
          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 */
              memcpy(dest, src, data);
              dest[data] = '\0'; /* NULL terminate */
          }
          printLine(dest);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE194_Unexpected_Sign_Extension__negative_memcpy_51b_goodG2B_sink(short data)
  {
      {
          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 */
              memcpy(dest, src, data);
              dest[data] = '\0'; /* NULL terminate */
          }
          printLine(dest);
      }
  }
  
  #endif /* OMITGOOD */
  