text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE114_Process_Control__w32_char_relativePath_05.c
  Label Definition File: CWE114_Process_Control__w32.label.xml
  Template File: sources-sink-05.tmpl.c
  */
  /*
   * @description
   * CWE: 114 Process Control
   * BadSource: relativePath Hard code the relative pathname to the library
   * GoodSource: Hard code the full pathname to the library
   * Sink:
   *    BadSink : Load a dynamic link library
   * Flow Variant: 05 Control flow: if(static_t) and if(static_f)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #include <windows.h>
  
  /* The two variables below are not defined as "const", but are never
     assigned any other value, so a tool should be able to identify that
     reads of these will always return their initialized values. */
  static int static_t = 1; /* true */
  static int static_f = 0; /* false */
  
  #ifndef OMITBAD
  
  void CWE114_Process_Control__w32_char_relativePath_05_bad()
  {
      char * data;
      char data_buf[100] = "";
      data = data_buf;
      if(static_t)
      {
          /* FLAW: Specify just the file name for the library, not the full path */
          strcpy(data, "winsrv.dll");
      }
      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 static_t to static_f */
  static void goodG2B1()
  {
      char * data;
      char data_buf[100] = "";
      data = data_buf;
      if(static_f)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FLAW: Specify just the file name for the library, not the full path */
          strcpy(data, "winsrv.dll");
      }
      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(static_t)
      {
          /* 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 */
          /* FLAW: Specify just the file name for the library, not the full path */
          strcpy(data, "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
  }
  
  void CWE114_Process_Control__w32_char_relativePath_05_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_relativePath_05_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE114_Process_Control__w32_char_relativePath_05_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  