text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE23_Relative_Path_Traversal__wchar_t_console_w32CreateFile_16.cpp
  Label Definition File: CWE23_Relative_Path_Traversal.label.xml
  Template File: sources-sink-16.tmpl.cpp
  */
  /*
   * @description
   * CWE: 23 Relative Path Traversal
   * BadSource: console Read input from the console
   * GoodSource: Use a fixed file name
   * Sink: w32CreateFile
   *    BadSink : Open the file named in data using CreateFile()
   * Flow Variant: 16 Control flow: while(1)
   *
   * */
  
  #include "std_testcase.h"
  
  #ifdef _WIN32
  #define BASEPATH L"c:\\temp\\"
  #else
  #include <wchar.h>
  #define BASEPATH L"/tmp/"
  #endif
  
  #include <windows.h>
  
  namespace CWE23_Relative_Path_Traversal__wchar_t_console_w32CreateFile_16
  {
  
  #ifndef OMITBAD
  
  void bad()
  {
      wchar_t * data;
      wchar_t dataBuffer[FILENAME_MAX] = BASEPATH;
      data = dataBuffer;
      while(1)
      {
          {
              /* Read input from the console */
              size_t dataLen = wcslen(data);
              /* if there is room in data, read into it from the console */
              if (FILENAME_MAX-dataLen > 1)
              {
                  /* POTENTIAL FLAW: Read data from the console */
                  if (fgetws(data+dataLen, (int)(FILENAME_MAX-dataLen), stdin) != NULL)
                  {
                      /* The next few lines remove the carriage return from the string that is
                       * inserted by fgetws() */
                      dataLen = wcslen(data);
                      if (dataLen > 0 && data[dataLen-1] == L'\n')
                      {
                          data[dataLen-1] = L'\0';
                      }
                  }
                  else
                  {
                      printLine("fgetws() failed");
                      /* Restore NUL terminator if fgetws fails */
                      data[dataLen] = L'\0';
                  }
              }
          }
          break;
      }
      {
          HANDLE hFile;
          /* POTENTIAL FLAW: Possibly creating and opening a file without validating the file name or path */
          hFile = CreateFileW(data,
                              (GENERIC_WRITE|GENERIC_READ),
                              0,
                              NULL,
                              OPEN_ALWAYS,
                              FILE_ATTRIBUTE_NORMAL,
                              NULL);
          if (hFile != INVALID_HANDLE_VALUE)
          {
              CloseHandle(hFile);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() - use goodsource and badsink by changing the conditions on the while statements */
  static void goodG2B()
  {
      wchar_t * data;
      wchar_t dataBuffer[FILENAME_MAX] = BASEPATH;
      data = dataBuffer;
      while(1)
      {
          /* FIX: Use a fixed file name */
          wcscat(data, L"file.txt");
          break;
      }
      {
          HANDLE hFile;
          /* POTENTIAL FLAW: Possibly creating and opening a file without validating the file name or path */
          hFile = CreateFileW(data,
                              (GENERIC_WRITE|GENERIC_READ),
                              0,
                              NULL,
                              OPEN_ALWAYS,
                              FILE_ATTRIBUTE_NORMAL,
                              NULL);
          if (hFile != INVALID_HANDLE_VALUE)
          {
              CloseHandle(hFile);
          }
      }
  }
  
  void good()
  {
      goodG2B();
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  
  /* 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
  
  using namespace CWE23_Relative_Path_Traversal__wchar_t_console_w32CreateFile_16; /* so that we can use good and bad easily */
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  