text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE78_OS_Command_Injection__char_environment_system_43.cpp
  Label Definition File: CWE78_OS_Command_Injection.one_string.label.xml
  Template File: sources-sink-43.tmpl.cpp
  */
  /*
   * @description
   * CWE: 78 OS Command Injection
   * BadSource: environment Read input from an environment variable
   * GoodSource: Fixed string
   * Sinks: system
   *    BadSink : Execute command in data using system()
   * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifdef _WIN32
  #define FULL_COMMAND "%WINDIR%\\system32\\cmd.exe /c dir "
  #else
  #include <unistd.h>
  #define FULL_COMMAND "/bin/sh ls -la "
  #endif
  
  #define ENV_VARIABLE "ADD"
  
  #ifdef _WIN32
  #define GETENV getenv
  #else
  #define GETENV getenv
  #endif
  
  #ifdef _WIN32
  #define SYSTEM system
  #else /* NOT _WIN32 */
  #define SYSTEM system
  #endif
  
  namespace CWE78_OS_Command_Injection__char_environment_system_43
  {
  
  #ifndef OMITBAD
  
  static void badSource(char * &data)
  {
      {
          /* Append input from an environment variable to data */
          size_t dataLen = strlen(data);
          char * environment = GETENV(ENV_VARIABLE);
          /* If there is data in the environment variable */
          if (environment != NULL)
          {
              /* POTENTIAL FLAW: Read data from an environment variable */
              strncat(data+dataLen, environment, 100-dataLen-1);
          }
      }
  }
  
  void bad()
  {
      char * data;
      char data_buf[100] = FULL_COMMAND;
      data = data_buf;
      badSource(data);
      /* POTENTIAL FLAW: Execute command in data possibly leading to command injection */
      if (SYSTEM(data) <= 0)
      {
          printLine("command execution failed!");
          exit(1);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  static void goodG2BSource(char * &data)
  {
      /* FIX: Append a fixed string to data (not user / external input) */
      strcat(data, "*.*");
  }
  
  static void goodG2B()
  {
      char * data;
      char data_buf[100] = FULL_COMMAND;
      data = data_buf;
      goodG2BSource(data);
      /* POTENTIAL FLAW: Execute command in data possibly leading to command injection */
      if (SYSTEM(data) <= 0)
      {
          printLine("command execution failed!");
          exit(1);
      }
  }
  
  void good()
  {
      goodG2B();
  }
  
  #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 CWE78_OS_Command_Injection__char_environment_system_43; /* 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
  