text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63a.c
  Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml
  Template File: sources-vasinks-63a.tmpl.c
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: environment Read input from an environment variable
   * GoodSource: Copy a fixed string into data
   * Sinks: vprintf
   *    GoodSink: vwprintf with a format string
   *    BadSink : vwprintf without a format string
   * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files
   *
   * */
  
  #include <stdarg.h>
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #define ENV_VARIABLE L"ADD"
  
  #ifdef _WIN32
  #define GETENV _wgetenv
  #else
  #define GETENV getenv
  #endif
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63b_badSink(wchar_t * * dataPtr);
  
  void CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63_bad()
  {
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      {
          /* Append input from an environment variable to data */
          size_t dataLen = wcslen(data);
          wchar_t * environment = GETENV(ENV_VARIABLE);
          /* If there is data in the environment variable */
          if (environment != NULL)
          {
              /* POTENTIAL FLAW: Read data from an environment variable */
              wcsncat(data+dataLen, environment, 100-dataLen-1);
          }
      }
      CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63b_badSink(&data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63b_goodG2BSink(wchar_t * * data);
  
  static void goodG2B()
  {
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      /* FIX: Use a fixed string that does not contain a format specifier */
      wcscpy(data, L"fixedstringtest");
      CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63b_goodG2BSink(&data);
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63b_goodB2GSink(wchar_t * * data);
  
  static void goodB2G()
  {
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      {
          /* Append input from an environment variable to data */
          size_t dataLen = wcslen(data);
          wchar_t * environment = GETENV(ENV_VARIABLE);
          /* If there is data in the environment variable */
          if (environment != NULL)
          {
              /* POTENTIAL FLAW: Read data from an environment variable */
              wcsncat(data+dataLen, environment, 100-dataLen-1);
          }
      }
      CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63b_goodB2GSink(&data);
  }
  
  void CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63_good()
  {
      goodG2B();
      goodB2G();
  }
  
  #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()...");
      CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE134_Uncontrolled_Format_String__wchar_t_environment_vprintf_63_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  