text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE36_Absolute_Path_Traversal__char_Environment_fopen_07.c
  Label Definition File: CWE36_Absolute_Path_Traversal.label.xml
  Template File: sources-sink-07.tmpl.c
  */
  /*
   * @description
   * CWE: 36 Absolute Path Traversal
   * BadSource: Environment Read input from an environment variable
   * GoodSource: Full path and file name
   * Sink: fopen
   *    BadSink :
   * Flow Variant: 07 Control flow: if(static_five==5) and if(static_five!=5)
   *
   * */
  
  #include "std_testcase.h"
  
  #define ENV_VARIABLE "ADD"
  
  #ifdef _WIN32
  # define GETENV getenv
  #else
  # define GETENV getenv
  #endif
  
  #ifdef _WIN32
  # define FOPEN fopen
  #else
  /* fopen is used on unix-based OSs */
  # define FOPEN fopen
  #endif
  
  /* The variable below is not declared "const", but is never assigned
     any other value so a tool should be able to identify that reads of
     this will always give its initialized value. */
  static int static_five = 5;
  
  #ifndef OMITBAD
  
  void CWE36_Absolute_Path_Traversal__char_Environment_fopen_07_bad()
  {
      char * data;
      char data_buf[FILENAME_MAX] = "";
      data = data_buf;
      if(static_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 */
  #ifdef _WIN32
          /* FIX: Full path and file name */
          strcpy(data, "c:\\temp\\file.txt");
  #else
          /* FIX: Full path and file name */
          strcpy(data, "/tmp/file.txt");
  #endif
      }
      {
          FILE *file = NULL;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          file = FOPEN(data, "wb+");
          if (file != NULL) fclose(file);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the static_five==5 to static_five!=5 */
  static void goodG2B1()
  {
      char * data;
      char data_buf[FILENAME_MAX] = "";
      data = data_buf;
      if(static_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
      {
  #ifdef _WIN32
          /* FIX: Full path and file name */
          strcpy(data, "c:\\temp\\file.txt");
  #else
          /* FIX: Full path and file name */
          strcpy(data, "/tmp/file.txt");
  #endif
      }
      {
          FILE *file = NULL;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          file = FOPEN(data, "wb+");
          if (file != NULL) fclose(file);
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      char * data;
      char data_buf[FILENAME_MAX] = "";
      data = data_buf;
      if(static_five==5)
      {
  #ifdef _WIN32
          /* FIX: Full path and file name */
          strcpy(data, "c:\\temp\\file.txt");
  #else
          /* FIX: Full path and file name */
          strcpy(data, "/tmp/file.txt");
  #endif
      }
      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);
              }
          }
      }
      {
          FILE *file = NULL;
          /* POTENTIAL FLAW: Possibly opening a file without validating the file name or path */
          file = FOPEN(data, "wb+");
          if (file != NULL) fclose(file);
      }
  }
  
  void CWE36_Absolute_Path_Traversal__char_Environment_fopen_07_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()...");
      CWE36_Absolute_Path_Traversal__char_Environment_fopen_07_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE36_Absolute_Path_Traversal__char_Environment_fopen_07_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  