text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE36_Absolute_Path_Traversal__wchar_t_Environment_fopen_10.c
  Label Definition File: CWE36_Absolute_Path_Traversal.label.xml
  Template File: sources-sink-10.tmpl.c
  */
  /*
   * @description
   * CWE: 36 Absolute Path Traversal
   * BadSource: Environment Read input from an environment variable
   * GoodSource: Full path and file name
   * Sink: fopen
   *    BadSink :
   * Flow Variant: 10 Control flow: if(global_t) and if(global_f)
   *
   * */
  
  #include "std_testcase.h"
  
  #define ENV_VARIABLE L"ADD"
  
  #ifdef _WIN32
  # define GETENV _wgetenv
  #else
  # define GETENV wgetenv
  #endif
  
  #ifdef _WIN32
  # define FOPEN _wfopen
  #else
  /* fopen is used on unix-based OSs */
  # define FOPEN fopen
  #endif
  
  #ifndef OMITBAD
  
  void CWE36_Absolute_Path_Traversal__wchar_t_Environment_fopen_10_bad()
  {
      wchar_t * data;
      wchar_t data_buf[FILENAME_MAX] = L"";
      data = data_buf;
      if(global_t)
      {
          {
              /* Read input from an environment variable */
              size_t data_len = wcslen(data);
              wchar_t * environment = GETENV(ENV_VARIABLE);
              /* If there is data in the environment variable */
              if (environment != NULL)
              {
                  wcsncat(data+data_len, environment, 100-data_len-1);
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
  #ifdef _WIN32
          /* FIX: Full path and file name */
          wcscpy(data, L"c:\\temp\\file.txt");
  #else
          /* FIX: Full path and file name */
          wcscpy(data, L"/tmp/file.txt");
  #endif
      }
      {
          FILE *file = NULL;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          file = FOPEN(data, L"wb+");
          if (file != NULL) fclose(file);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the global_t to global_f */
  static void goodG2B1()
  {
      wchar_t * data;
      wchar_t data_buf[FILENAME_MAX] = L"";
      data = data_buf;
      if(global_f)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              /* Read input from an environment variable */
              size_t data_len = wcslen(data);
              wchar_t * environment = GETENV(ENV_VARIABLE);
              /* If there is data in the environment variable */
              if (environment != NULL)
              {
                  wcsncat(data+data_len, environment, 100-data_len-1);
              }
          }
      }
      else
      {
  #ifdef _WIN32
          /* FIX: Full path and file name */
          wcscpy(data, L"c:\\temp\\file.txt");
  #else
          /* FIX: Full path and file name */
          wcscpy(data, L"/tmp/file.txt");
  #endif
      }
      {
          FILE *file = NULL;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          file = FOPEN(data, L"wb+");
          if (file != NULL) fclose(file);
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      wchar_t * data;
      wchar_t data_buf[FILENAME_MAX] = L"";
      data = data_buf;
      if(global_t)
      {
  #ifdef _WIN32
          /* FIX: Full path and file name */
          wcscpy(data, L"c:\\temp\\file.txt");
  #else
          /* FIX: Full path and file name */
          wcscpy(data, L"/tmp/file.txt");
  #endif
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              /* Read input from an environment variable */
              size_t data_len = wcslen(data);
              wchar_t * environment = GETENV(ENV_VARIABLE);
              /* If there is data in the environment variable */
              if (environment != NULL)
              {
                  wcsncat(data+data_len, environment, 100-data_len-1);
              }
          }
      }
      {
          FILE *file = NULL;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          file = FOPEN(data, L"wb+");
          if (file != NULL) fclose(file);
      }
  }
  
  void CWE36_Absolute_Path_Traversal__wchar_t_Environment_fopen_10_good()
  {
      goodG2B1();
      goodG2B2();
  }
  
  #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()...");
      CWE36_Absolute_Path_Traversal__wchar_t_Environment_fopen_10_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE36_Absolute_Path_Traversal__wchar_t_Environment_fopen_10_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  