text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE23_Relative_Path_Traversal__wchar_t_fromFile_open_53a.c
  Label Definition File: CWE23_Relative_Path_Traversal.label.xml
  Template File: sources-sink-53a.tmpl.c
  */
  /*
   * @description
   * CWE: 23 Relative Path Traversal
   * BadSource: fromFile Read input from a file
   * GoodSource: File name without a period or slash
   * Sink: open
   *    BadSink :
   * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #ifdef _WIN32
  #define BASEPATH L"c:\\temp\\"
  #else
  #define BASEPATH L"/tmp/"
  #endif
  
  #ifdef _WIN32
  # define FOPEN _wfopen
  #else
  /* fopen is used on unix-based OSs */
  # define FOPEN fopen
  #endif
  
  #ifdef _WIN32
  # define OPEN _wopen
  # define CLOSE _close
  #else
  # define OPEN wopen
  # define CLOSE close
  #endif
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void CWE23_Relative_Path_Traversal__wchar_t_fromFile_open_53b_bad_sink(wchar_t * data);
  
  void CWE23_Relative_Path_Traversal__wchar_t_fromFile_open_53_bad()
  {
      wchar_t * data;
      wchar_t data_buf[FILENAME_MAX] = BASEPATH;
      data = data_buf;
      {
          /* Read input from a file */
          size_t data_len = wcslen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if(FILENAME_MAX-data_len > 1)
          {
              pFile = FOPEN(L"C:\\temp\\file.txt", L"r");
              if (pFile != NULL)
              {
                  fgetws(data+data_len, (int)(FILENAME_MAX-data_len), pFile);
                  fclose(pFile);
              }
          }
      }
      CWE23_Relative_Path_Traversal__wchar_t_fromFile_open_53b_bad_sink(data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* good function declaration */
  void CWE23_Relative_Path_Traversal__wchar_t_fromFile_open_53b_goodG2B_sink(wchar_t * data);
  
  /* goodG2B uses the GoodSource with the BadSink */
  static void goodG2B()
  {
      wchar_t * data;
      wchar_t data_buf[FILENAME_MAX] = BASEPATH;
      data = data_buf;
      /* FIX: File name does not contain a period or slash */
      wcscat(data, L"file.txt");
      CWE23_Relative_Path_Traversal__wchar_t_fromFile_open_53b_goodG2B_sink(data);
  }
  
  void CWE23_Relative_Path_Traversal__wchar_t_fromFile_open_53_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()...");
      CWE23_Relative_Path_Traversal__wchar_t_fromFile_open_53_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE23_Relative_Path_Traversal__wchar_t_fromFile_open_53_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  