text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE427_Uncontrolled_Search_Path_Element__char_fromFile_15.c
  Label Definition File: CWE427_Uncontrolled_Search_Path_Element.label.xml
  Template File: sources-sink-15.tmpl.c
  */
  /*
   * @description
   * CWE: 427 Uncontrolled Search Path Element
   * BadSource: fromFile Read input from a file
   * GoodSource: Use a hardcoded path
   * Sink:
   *    BadSink : Set the environment variable
   * Flow Variant: 15 Control flow: switch(6)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifdef _WIN32
  # define NEW_PATH "%SystemRoot%\\system32"
  # define PUTENV _putenv
  #else
  # define NEW_PATH "/bin"
  # define PUTENV putenv
  #endif
  
  #ifdef _WIN32
  # define FOPEN fopen
  #else
  /* fopen is used on unix-based OSs */
  # define FOPEN fopen
  #endif
  
  #ifndef OMITBAD
  
  void CWE427_Uncontrolled_Search_Path_Element__char_fromFile_15_bad()
  {
      char * data;
      char data_buf[250] = "PATH=";
      data = data_buf;
      switch(6)
      {
      case 6:
      {
          /* Read input from a file */
          size_t data_len = strlen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if(250-data_len > 1)
          {
              pFile = FOPEN("C:\\temp\\file.txt", "r");
              if (pFile != NULL)
              {
                  fgets(data+data_len, (int)(250-data_len), pFile);
                  fclose(pFile);
              }
          }
      }
      break;
      default:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Set the path as the "system" path */
          strcat(data, NEW_PATH);
          break;
      }
      /* POTENTIAL FLAW: Set a new environment variable with a path that is possibly insecure */
      PUTENV(data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the switch to switch(5) */
  static void goodG2B1()
  {
      char * data;
      char data_buf[250] = "PATH=";
      data = data_buf;
      switch(5)
      {
      case 6:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
      {
          /* Read input from a file */
          size_t data_len = strlen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if(250-data_len > 1)
          {
              pFile = FOPEN("C:\\temp\\file.txt", "r");
              if (pFile != NULL)
              {
                  fgets(data+data_len, (int)(250-data_len), pFile);
                  fclose(pFile);
              }
          }
      }
      break;
      default:
          /* FIX: Set the path as the "system" path */
          strcat(data, NEW_PATH);
          break;
      }
      /* POTENTIAL FLAW: Set a new environment variable with a path that is possibly insecure */
      PUTENV(data);
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the switch */
  static void goodG2B2()
  {
      char * data;
      char data_buf[250] = "PATH=";
      data = data_buf;
      switch(6)
      {
      case 6:
          /* FIX: Set the path as the "system" path */
          strcat(data, NEW_PATH);
          break;
      default:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
      {
          /* Read input from a file */
          size_t data_len = strlen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if(250-data_len > 1)
          {
              pFile = FOPEN("C:\\temp\\file.txt", "r");
              if (pFile != NULL)
              {
                  fgets(data+data_len, (int)(250-data_len), pFile);
                  fclose(pFile);
              }
          }
      }
      break;
      }
      /* POTENTIAL FLAW: Set a new environment variable with a path that is possibly insecure */
      PUTENV(data);
  }
  
  void CWE427_Uncontrolled_Search_Path_Element__char_fromFile_15_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()...");
      CWE427_Uncontrolled_Search_Path_Element__char_fromFile_15_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE427_Uncontrolled_Search_Path_Element__char_fromFile_15_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  