text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE114_Process_Control__w32_char_console_52a.c
  Label Definition File: CWE114_Process_Control__w32.label.xml
  Template File: sources-sink-52a.tmpl.c
  */
  /*
   * @description
   * CWE: 114 Process Control
   * BadSource: console Read input from the console
   * GoodSource: Hard code the full pathname to the library
   * Sink:
   *    BadSink : Load a dynamic link library
   * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #include <windows.h>
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void CWE114_Process_Control__w32_char_console_52b_badSink(char * data);
  
  void CWE114_Process_Control__w32_char_console_52_bad()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      {
          /* Read input from the console */
          size_t dataLen = strlen(data);
          /* if there is room in data, read into it from the console */
          if (100-dataLen > 1)
          {
              /* POTENTIAL FLAW: Read data from the console */
              if (fgets(data+dataLen, (int)(100-dataLen), stdin) != NULL)
              {
                  /* The next few lines remove the carriage return from the string that is
                   * inserted by fgets() */
                  dataLen = strlen(data);
                  if (dataLen > 0 && data[dataLen-1] == '\n')
                  {
                      data[dataLen-1] = '\0';
                  }
              }
              else
              {
                  printLine("fgets() failed");
                  /* Restore NUL terminator if fgets fails */
                  data[dataLen] = '\0';
              }
          }
      }
      CWE114_Process_Control__w32_char_console_52b_badSink(data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* good function declaration */
  void CWE114_Process_Control__w32_char_console_52b_goodG2BSink(char * data);
  
  /* goodG2B uses the GoodSource with the BadSink */
  static void goodG2B()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      /* FIX: Specify the full pathname for the library */
      strcpy(data, "C:\\Windows\\System32\\winsrv.dll");
      CWE114_Process_Control__w32_char_console_52b_goodG2BSink(data);
  }
  
  void CWE114_Process_Control__w32_char_console_52_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()...");
      CWE114_Process_Control__w32_char_console_52_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE114_Process_Control__w32_char_console_52_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  