text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE78_OS_Command_Injection__char_file_w32_execvp_61b.c
  Label Definition File: CWE78_OS_Command_Injection.strings.label.xml
  Template File: sources-sink-61b.tmpl.c
  */
  /*
   * @description
   * CWE: 78 OS Command Injection
   * BadSource: file Read input from a file
   * GoodSource: Fixed string
   * Sinks: w32_execvp
   *    BadSink : execute command with execvp
   * Flow Variant: 61 Data flow: data returned from one function to another in different source files
   *
   * */
  
  #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 "ls"
  #define COMMAND_ARG2 "-la"
  #define COMMAND_ARG3 data
  #endif
  
  #ifdef _WIN32
  #define FILENAME "C:\\temp\\file.txt"
  #else
  #define FILENAME "/tmp/file.txt"
  #endif
  
  #include <process.h>
  #define EXECVP _execvp
  
  #ifndef OMITBAD
  
  char * CWE78_OS_Command_Injection__char_file_w32_execvp_61b_badSource(char * data)
  {
      {
          /* Read input from a file */
          size_t dataLen = strlen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if (100-dataLen > 1)
          {
              pFile = fopen(FILENAME, "r");
              if (pFile != NULL)
              {
                  /* POTENTIAL FLAW: Read data from a file */
                  if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)
                  {
                      printLine("fgets() failed");
                      /* Restore NUL terminator if fgets fails */
                      data[dataLen] = '\0';
                  }
                  fclose(pFile);
              }
          }
      }
      return data;
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  char * CWE78_OS_Command_Injection__char_file_w32_execvp_61b_goodG2BSource(char * data)
  {
      /* FIX: Append a fixed string to data (not user / external input) */
      strcat(data, "*.*");
      return data;
  }
  
  #endif /* OMITGOOD */
  