text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE36_Absolute_Path_Traversal__wchar_t_environment_open_66b.cpp
  Label Definition File: CWE36_Absolute_Path_Traversal.label.xml
  Template File: sources-sink-66b.tmpl.cpp
  */
  /*
   * @description
   * CWE: 36 Absolute Path Traversal
   * BadSource: environment Read input from an environment variable
   * GoodSource: Full path and file name
   * Sinks: open
   *    BadSink : Open the file named in data using open()
   * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #define ENV_VARIABLE L"ADD"
  
  #ifdef _WIN32
  #define GETENV _wgetenv
  #else
  #define GETENV getenv
  #endif
  
  #ifdef _WIN32
  #define OPEN _wopen
  #define CLOSE _close
  #else
  #include <unistd.h>
  #define OPEN open
  #define CLOSE close
  #endif
  
  namespace CWE36_Absolute_Path_Traversal__wchar_t_environment_open_66
  {
  
  #ifndef OMITBAD
  
  void badSink(wchar_t * dataArray[])
  {
      /* copy data out of dataArray */
      wchar_t * data = dataArray[2];
      {
          int fileDesc;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          fileDesc = OPEN(data, O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
          if (fileDesc != -1)
          {
              CLOSE(fileDesc);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(wchar_t * dataArray[])
  {
      wchar_t * data = dataArray[2];
      {
          int fileDesc;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          fileDesc = OPEN(data, O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
          if (fileDesc != -1)
          {
              CLOSE(fileDesc);
          }
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  