text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE78_OS_Command_Injection__char_console_execlp_02.c
  Label Definition File: CWE78_OS_Command_Injection.strings.label.xml
  Template File: sources-sink-02.tmpl.c
  */
  /*
   * @description
   * CWE: 78 OS Command Injection
   * BadSource: console Read input from the console
   * GoodSource: Fixed string
   * Sink: execlp
   *    BadSink : execute command with execlp
   * Flow Variant: 02 Control flow: if(1) and if(0)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifdef _WIN32
  #define COMMAND_INT_PATH "%WINDIR%\\system32\\cmd.exe"
  #define COMMAND_INT "cmd.exe"
  #define COMMAND_ARG1 "/c"
  #define COMMAND_ARG2 "dir "
  #define COMMAND_ARG3 data
  #else /* NOT _WIN32 */
  #include <unistd.h>
  #define COMMAND_INT_PATH "/bin/sh"
  #define COMMAND_INT "sh"
  #define COMMAND_ARG1 "-c"
  #define COMMAND_ARG2 "ls "
  #define COMMAND_ARG3 data
  #endif
  
  #ifdef _WIN32
  #include <process.h>
  #define EXECLP _execlp
  #else /* NOT _WIN32 */
  #define EXECLP execlp
  #endif
  
  #ifndef OMITBAD
  
  void CWE78_OS_Command_Injection__char_console_execlp_02_bad()
  {
      char * data;
      char dataBuffer[100] = COMMAND_ARG2;
      data = dataBuffer;
      if(1)
      {
          {
              /* Read input from the console */
              size_t dataLen = strlen(data);
              /* if there is room in data, read into it from the console */
              if (100-dataLen > 1)
              {
                  /* POTENTIAL FLAW: Read data from the console */
                  if (fgets(data+dataLen, (int)(100-dataLen), stdin) != NULL)
                  {
                      /* The next few lines remove the carriage return from the string that is
                       * inserted by fgets() */
                      dataLen = strlen(data);
                      if (dataLen > 0 && data[dataLen-1] == '\n')
                      {
                          data[dataLen-1] = '\0';
                      }
                  }
                  else
                  {
                      printLine("fgets() failed");
                      /* Restore NUL terminator if fgets fails */
                      data[dataLen] = '\0';
                  }
              }
          }
      }
      /* execlp - searches for the location of the command among
       * the directories specified by the PATH environment variable */
      /* POTENTIAL FLAW: Execute command without validating input possibly leading to command injection */
      EXECLP(COMMAND_INT, COMMAND_INT, COMMAND_ARG1, COMMAND_ARG3, NULL);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the 1 to 0 */
  static void goodG2B1()
  {
      char * data;
      char dataBuffer[100] = COMMAND_ARG2;
      data = dataBuffer;
      if(0)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: Append a fixed string to data (not user / external input) */
          strcat(data, "*.*");
      }
      /* execlp - searches for the location of the command among
       * the directories specified by the PATH environment variable */
      /* POTENTIAL FLAW: Execute command without validating input possibly leading to command injection */
      EXECLP(COMMAND_INT, COMMAND_INT, COMMAND_ARG1, COMMAND_ARG3, NULL);
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      char * data;
      char dataBuffer[100] = COMMAND_ARG2;
      data = dataBuffer;
      if(1)
      {
          /* FIX: Append a fixed string to data (not user / external input) */
          strcat(data, "*.*");
      }
      /* execlp - searches for the location of the command among
       * the directories specified by the PATH environment variable */
      /* POTENTIAL FLAW: Execute command without validating input possibly leading to command injection */
      EXECLP(COMMAND_INT, COMMAND_INT, COMMAND_ARG1, COMMAND_ARG3, NULL);
  }
  
  void CWE78_OS_Command_Injection__char_console_execlp_02_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()...");
      CWE78_OS_Command_Injection__char_console_execlp_02_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE78_OS_Command_Injection__char_console_execlp_02_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  