text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE36_Absolute_Path_Traversal__char_fromConsole_fopen_12.c
  Label Definition File: CWE36_Absolute_Path_Traversal.label.xml
  Template File: sources-sink-12.tmpl.c
  */
  /*
   * @description
   * CWE: 36 Absolute Path Traversal
   * BadSource: fromConsole Read input from the console
   * GoodSource: Full path and file name
   * Sink: fopen
   *    BadSink :
   * Flow Variant: 12 Control flow: if(global_returns_t_or_f())
   *
   * */
  
  #include "std_testcase.h"
  
  #ifdef _WIN32
  # define FOPEN fopen
  #else
  /* fopen is used on unix-based OSs */
  # define FOPEN fopen
  #endif
  
  #ifndef OMITBAD
  
  void CWE36_Absolute_Path_Traversal__char_fromConsole_fopen_12_bad()
  {
      char * data;
      char data_buf[FILENAME_MAX] = "";
      data = data_buf;
      if(global_returns_t_or_f())
      {
          {
              /* Read input from the console */
              size_t data_len = strlen(data);
              /* if there is room in data, read into it from the console */
              if(FILENAME_MAX-data_len > 1)
              {
                  fgets(data+data_len, (int)(FILENAME_MAX-data_len), stdin);
                  /* The next 3 lines remove the carriage return from the string that is
                   * inserted by fgets() */
                  data_len = strlen(data);
                  if (data_len > 0)
                  {
                      data[data_len-1] = '\0';
                  }
              }
          }
      }
      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);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() - use goodsource and badsink by changing the "if" so that
     both branches use the GoodSource */
  static void goodG2B()
  {
      char * data;
      char data_buf[FILENAME_MAX] = "";
      data = data_buf;
      if(global_returns_t_or_f())
      {
  #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
      {
  #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);
      }
  }
  
  void CWE36_Absolute_Path_Traversal__char_fromConsole_fopen_12_good()
  {
      goodG2B();
  }
  
  #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_fromConsole_fopen_12_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE36_Absolute_Path_Traversal__char_fromConsole_fopen_12_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  