text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE114_Process_Control__w32_char_Environment_14.c
  Label Definition File: CWE114_Process_Control__w32.label.xml
  Template File: sources-sink-14.tmpl.c
  */
  /*
   * @description
   * CWE: 114 Process Control
   * BadSource: Environment Read input from an environment variable
   * GoodSource: Hard code the full pathname to the library
   * Sink:
   *    BadSink : Load a dynamic link library
   * Flow Variant: 14 Control flow: if(global_five==5) and if(global_five!=5)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #define ENV_VARIABLE "ADD"
  
  #ifdef _WIN32
  # define GETENV getenv
  #else
  # define GETENV getenv
  #endif
  
  #include <windows.h>
  
  #ifndef OMITBAD
  
  void CWE114_Process_Control__w32_char_Environment_14_bad()
  {
      char * data;
      char data_buf[100] = "";
      data = data_buf;
      if(global_five==5)
      {
          {
              /* Read input from an environment variable */
              size_t data_len = strlen(data);
              char * environment = GETENV(ENV_VARIABLE);
              /* If there is data in the environment variable */
              if (environment != NULL)
              {
                  strncat(data+data_len, environment, 100-data_len-1);
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Specify the full pathname for the library */
          strcpy(data, "C:\\Windows\\System32\\winsrv.dll");
      }
  #ifdef _WIN32
      {
          HMODULE hModule;
          /* POTENTIAL FLAW: If the path to the library is not specified, an attacker may be able to
           * replace his own file with the intended library */
          hModule = LoadLibraryA(data);
          if (hModule != NULL)
          {
              FreeLibrary(hModule);
              printLine("Library loaded and freed successfully");
          }
          else
          {
              printLine("Unable to load library");
          }
      }
  #endif
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the global_five==5 to global_five!=5 */
  static void goodG2B1()
  {
      char * data;
      char data_buf[100] = "";
      data = data_buf;
      if(global_five!=5)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              /* Read input from an environment variable */
              size_t data_len = strlen(data);
              char * environment = GETENV(ENV_VARIABLE);
              /* If there is data in the environment variable */
              if (environment != NULL)
              {
                  strncat(data+data_len, environment, 100-data_len-1);
              }
          }
      }
      else
      {
          /* FIX: Specify the full pathname for the library */
          strcpy(data, "C:\\Windows\\System32\\winsrv.dll");
      }
  #ifdef _WIN32
      {
          HMODULE hModule;
          /* POTENTIAL FLAW: If the path to the library is not specified, an attacker may be able to
           * replace his own file with the intended library */
          hModule = LoadLibraryA(data);
          if (hModule != NULL)
          {
              FreeLibrary(hModule);
              printLine("Library loaded and freed successfully");
          }
          else
          {
              printLine("Unable to load library");
          }
      }
  #endif
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      char * data;
      char data_buf[100] = "";
      data = data_buf;
      if(global_five==5)
      {
          /* FIX: Specify the full pathname for the library */
          strcpy(data, "C:\\Windows\\System32\\winsrv.dll");
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              /* Read input from an environment variable */
              size_t data_len = strlen(data);
              char * environment = GETENV(ENV_VARIABLE);
              /* If there is data in the environment variable */
              if (environment != NULL)
              {
                  strncat(data+data_len, environment, 100-data_len-1);
              }
          }
      }
  #ifdef _WIN32
      {
          HMODULE hModule;
          /* POTENTIAL FLAW: If the path to the library is not specified, an attacker may be able to
           * replace his own file with the intended library */
          hModule = LoadLibraryA(data);
          if (hModule != NULL)
          {
              FreeLibrary(hModule);
              printLine("Library loaded and freed successfully");
          }
          else
          {
              printLine("Unable to load library");
          }
      }
  #endif
  }
  
  void CWE114_Process_Control__w32_char_Environment_14_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()...");
      CWE114_Process_Control__w32_char_Environment_14_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE114_Process_Control__w32_char_Environment_14_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  