text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE23_Relative_Path_Traversal__wchar_t_environment_fopen_09.cpp
  Label Definition File: CWE23_Relative_Path_Traversal.label.xml
  Template File: sources-sink-09.tmpl.cpp
  */
  /*
   * @description
   * CWE: 23 Relative Path Traversal
   * BadSource: environment Read input from an environment variable
   * GoodSource: Use a fixed file name
   * Sink: fopen
   *    BadSink : Open the file named in data using fopen()
   * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)
   *
   * */
  
  #include "std_testcase.h"
  
  #ifdef _WIN32
  #define BASEPATH L"c:\\temp\\"
  #else
  #include <wchar.h>
  #define BASEPATH L"/tmp/"
  #endif
  
  #define ENV_VARIABLE L"ADD"
  
  #ifdef _WIN32
  #define GETENV _wgetenv
  #else
  #define GETENV getenv
  #endif
  
  #ifdef _WIN32
  #define FOPEN _wfopen
  #else
  #define FOPEN fopen
  #endif
  
  namespace CWE23_Relative_Path_Traversal__wchar_t_environment_fopen_09
  {
  
  #ifndef OMITBAD
  
  void bad()
  {
      wchar_t * data;
      wchar_t dataBuffer[FILENAME_MAX] = BASEPATH;
      data = dataBuffer;
      if(GLOBAL_CONST_TRUE)
      {
          {
              /* Append input from an environment variable to data */
              size_t dataLen = wcslen(data);
              wchar_t * environment = GETENV(ENV_VARIABLE);
              /* If there is data in the environment variable */
              if (environment != NULL)
              {
                  /* POTENTIAL FLAW: Read data from an environment variable */
                  wcsncat(data+dataLen, environment, FILENAME_MAX-dataLen-1);
              }
          }
      }
      {
          FILE *pFile = NULL;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          pFile = FOPEN(data, L"wb+");
          if (pFile != NULL)
          {
              fclose(pFile);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */
  static void goodG2B1()
  {
      wchar_t * data;
      wchar_t dataBuffer[FILENAME_MAX] = BASEPATH;
      data = dataBuffer;
      if(GLOBAL_CONST_FALSE)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: Use a fixed file name */
          wcscat(data, L"file.txt");
      }
      {
          FILE *pFile = NULL;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          pFile = FOPEN(data, L"wb+");
          if (pFile != NULL)
          {
              fclose(pFile);
          }
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      wchar_t * data;
      wchar_t dataBuffer[FILENAME_MAX] = BASEPATH;
      data = dataBuffer;
      if(GLOBAL_CONST_TRUE)
      {
          /* FIX: Use a fixed file name */
          wcscat(data, L"file.txt");
      }
      {
          FILE *pFile = NULL;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          pFile = FOPEN(data, L"wb+");
          if (pFile != NULL)
          {
              fclose(pFile);
          }
      }
  }
  
  void good()
  {
      goodG2B1();
      goodG2B2();
  }
  
  #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_environment_fopen_09; /* 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
  