text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE194_Unexpected_Sign_Extension__fscanf_memmove_01.c
  Label Definition File: CWE194_Unexpected_Sign_Extension.label.xml
  Template File: sources-sink-01.tmpl.c
  */
  /*
   * @description
   * CWE: 194 Unexpected Sign Extension
   * BadSource: fscanf Read data from the console using fscanf()
   * GoodSource: Positive integer
   * Sink: memmove
   *    BadSink : Copy strings using memmove() with the length of data
   * Flow Variant: 01 Baseline
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef OMITBAD
  
  void CWE194_Unexpected_Sign_Extension__fscanf_memmove_01_bad()
  {
      short data;
      /* Initialize data */
      data = 0;
      /* POTENTIAL FLAW: Use a value input from the console using fscanf() */
      fscanf (stdin, "%hd", &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 */
              memmove(dest, src, data);
              dest[data] = '\0'; /* NULL terminate */
          }
          printLine(dest);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  static void goodG2B()
  {
      short data;
      /* Initialize data */
      data = 0;
      /* 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 extension could result in a very large number */
              memmove(dest, src, data);
              dest[data] = '\0'; /* NULL terminate */
          }
          printLine(dest);
      }
  }
  
  void CWE194_Unexpected_Sign_Extension__fscanf_memmove_01_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__fscanf_memmove_01_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE194_Unexpected_Sign_Extension__fscanf_memmove_01_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  