text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE194_Unexpected_Sign_Extension__fgets_memmove_12.c
  Label Definition File: CWE194_Unexpected_Sign_Extension.label.xml
  Template File: sources-sink-12.tmpl.c
  */
  /*
   * @description
   * CWE: 194 Unexpected Sign Extension
   * BadSource: fgets Read data from the console using fgets()
   * GoodSource: Positive integer
   * Sink: memmove
   *    BadSink : Copy strings using memmove() with the length of data
   * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())
   *
   * */
  
  #include "std_testcase.h"
  
  /* Must be at least 8 for atoi() to work properly */
  #define CHAR_ARRAY_SIZE 8
  
  #ifndef OMITBAD
  
  void CWE194_Unexpected_Sign_Extension__fgets_memmove_12_bad()
  {
      short data;
      /* Initialize data */
      data = 0;
      if(globalReturnsTrueOrFalse())
      {
          {
              char inputBuffer[CHAR_ARRAY_SIZE] = "";
              /* FLAW: Use a value input from the console using fgets() */
              if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)
              {
                  /* Convert to short */
                  data = (short)atoi(inputBuffer);
              }
              else
              {
                  printLine("fgets() failed.");
              }
          }
      }
      else
      {
          /* FIX: Use a positive integer less than &InitialDataSize&*/
          data = 100-1;
      }
      {
          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 extension could result in a very large number */
              memmove(dest, source, data);
              dest[data] = '\0'; /* NULL terminate */
          }
          printLine(dest);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() - use goodsource and badsink by changing the "if" so that
   * both branches use the GoodSource */
  static void goodG2B()
  {
      short data;
      /* Initialize data */
      data = 0;
      if(globalReturnsTrueOrFalse())
      {
          /* FIX: Use a positive integer less than &InitialDataSize&*/
          data = 100-1;
      }
      else
      {
          /* FIX: Use a positive integer less than &InitialDataSize&*/
          data = 100-1;
      }
      {
          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 extension could result in a very large number */
              memmove(dest, source, data);
              dest[data] = '\0'; /* NULL terminate */
          }
          printLine(dest);
      }
  }
  
  void CWE194_Unexpected_Sign_Extension__fgets_memmove_12_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__fgets_memmove_12_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE194_Unexpected_Sign_Extension__fgets_memmove_12_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  