text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE426_Untrusted_Search_Path__wchar_t_popen_03.c
  Label Definition File: CWE426_Untrusted_Search_Path.label.xml
  Template File: sources-sink-03.tmpl.c
  */
  /*
   * @description
   * CWE: 426 Untrusted Search Path
   * BadSource:  Don't specify the full path in the OS command
   * GoodSource: Specify the full path in the OS command
   * Sink: popen
   *    BadSink : Execute the wpopen function
   * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  #ifdef _WIN32
  #define BAD_OS_COMMAND L"cmd.exe /c dir"
  
  /* REVIEWERS NOTE:
   * This hard-coded path is probably incorrect for Windows systems, as it will
   * function improperly on 9x/NT/2K, possibly on XP+ systems upgraded from 2K,
   * and could introduce code execution vulnerabilities on Windows systems that
   * have a system drive on an alternate drive letter (e.g., d:\windows)
   */
  #define GOOD_OS_COMMAND L"c:\\windows\\system32\\cmd.exe /c dir"
  #else /* NOT _WIN32 */
  #define BAD_OS_COMMAND L"ls -la"
  #define GOOD_OS_COMMAND L"/usr/bin/ls -la"
  #endif /* end NOT _WIN32 */
  
  #ifdef _WIN32
  #define POPEN _wpopen
  #define PCLOSE _pclose
  #else /* NOT _WIN32 */
  #define POPEN popen
  #define PCLOSE pclose
  #endif
  
  #ifndef OMITBAD
  
  void CWE426_Untrusted_Search_Path__wchar_t_popen_03_bad()
  {
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      if(5==5)
      {
          /* FLAW: the full path is not specified */
          wcscpy(data, BAD_OS_COMMAND);
      }
      {
          FILE *pipe;
          /* POTENTIAL FLAW: Executing the wpopen() function without specifying the full path to the executable
           * can allow an attacker to run their own program */
          pipe = POPEN(data, L"wb");
          if (pipe != NULL)
          {
              PCLOSE(pipe);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the 5==5 to 5!=5 */
  static void goodG2B1()
  {
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      if(5!=5)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: full path is specified */
          wcscpy(data, GOOD_OS_COMMAND);
      }
      {
          FILE *pipe;
          /* POTENTIAL FLAW: Executing the wpopen() function without specifying the full path to the executable
           * can allow an attacker to run their own program */
          pipe = POPEN(data, L"wb");
          if (pipe != NULL)
          {
              PCLOSE(pipe);
          }
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      if(5==5)
      {
          /* FIX: full path is specified */
          wcscpy(data, GOOD_OS_COMMAND);
      }
      {
          FILE *pipe;
          /* POTENTIAL FLAW: Executing the wpopen() function without specifying the full path to the executable
           * can allow an attacker to run their own program */
          pipe = POPEN(data, L"wb");
          if (pipe != NULL)
          {
              PCLOSE(pipe);
          }
      }
  }
  
  void CWE426_Untrusted_Search_Path__wchar_t_popen_03_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()...");
      CWE426_Untrusted_Search_Path__wchar_t_popen_03_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE426_Untrusted_Search_Path__wchar_t_popen_03_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  