text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE78_OS_Command_Injection__wchar_t_file_system_62b.cpp
  Label Definition File: CWE78_OS_Command_Injection.one_string.label.xml
  Template File: sources-sink-62b.tmpl.cpp
  */
  /*
   * @description
   * CWE: 78 OS Command Injection
   * BadSource: file Read input from a file
   * GoodSource: Fixed string
   * Sinks: system
   *    BadSink : Execute command in data using system()
   * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifdef _WIN32
  #define FULL_COMMAND L"dir "
  #else
  #include <unistd.h>
  #define FULL_COMMAND L"ls "
  #endif
  
  #ifdef _WIN32
  #define FILENAME "C:\\temp\\file.txt"
  #else
  #define FILENAME "/tmp/file.txt"
  #endif
  
  namespace CWE78_OS_Command_Injection__wchar_t_file_system_62
  {
  
  #ifndef OMITBAD
  
  void badSource(wchar_t * &data)
  {
      {
          /* Read input from a file */
          size_t dataLen = wcslen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if (100-dataLen > 1)
          {
              pFile = fopen(FILENAME, "r");
              if (pFile != NULL)
              {
                  /* POTENTIAL FLAW: Read data from a file */
                  if (fgetws(data+dataLen, (int)(100-dataLen), pFile) == NULL)
                  {
                      printLine("fgetws() failed");
                      /* Restore NUL terminator if fgetws fails */
                      data[dataLen] = L'\0';
                  }
                  fclose(pFile);
              }
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  void goodG2BSource(wchar_t * &data)
  {
      /* FIX: Append a fixed string to data (not user / external input) */
      wcscat(data, L"*.*");
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  