text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE195_Signed_to_Unsigned_Conversion_Error__fgets_memcpy_54e.c
  Label Definition File: CWE195_Signed_to_Unsigned_Conversion_Error.label.xml
  Template File: sources-sink-54e.tmpl.c
  */
  /*
   * @description
   * CWE: 195 Signed to Unsigned Conversion Error
   * BadSource: fgets Read data from the console using fgets()
   * GoodSource: Positive integer
   * Sink: memcpy
   *    BadSink : Copy strings using memcpy() with the length of data
   * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */
  
  #ifndef OMITBAD
  
  void CWE195_Signed_to_Unsigned_Conversion_Error__fgets_memcpy_54e_badSink(int data)
  {
      {
          char source[100];
          char dest[100] = "";
          memset(source, 'A', 100-1);
          source[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 */
              memcpy(dest, source, data);
              dest[data] = '\0'; /* NULL terminate */
          }
          printLine(dest);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE195_Signed_to_Unsigned_Conversion_Error__fgets_memcpy_54e_goodG2BSink(int data)
  {
      {
          char source[100];
          char dest[100] = "";
          memset(source, 'A', 100-1);
          source[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 */
              memcpy(dest, source, data);
              dest[data] = '\0'; /* NULL terminate */
          }
          printLine(dest);
      }
  }
  
  #endif /* OMITGOOD */
  