text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__wchar_t_environment_snprintf_12.c
  Label Definition File: CWE134_Uncontrolled_Format_String.label.xml
  Template File: sources-sinks-12.tmpl.c
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: environment Read input from an environment variable
   * GoodSource: Copy a fixed string into data
   * Sinks: swprintf
   *    GoodSink: snwprintf with "%s" as the third argument and data as the fourth
   *    BadSink : snwprintf with data as the third argument
   * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())
   *
   * */
  
  #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
  
  #ifdef _WIN32
  #define SNPRINTF _snwprintf
  #else
  #define SNPRINTF swprintf
  #endif
  
  #ifndef OMITBAD
  
  void CWE134_Uncontrolled_Format_String__wchar_t_environment_snprintf_12_bad()
  {
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      if(globalReturnsTrueOrFalse())
      {
          {
              /* 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);
              }
          }
      }
      else
      {
          /* FIX: Use a fixed string that does not contain a format specifier */
          wcscpy(data, L"fixedstringtest");
      }
      if(globalReturnsTrueOrFalse())
      {
          {
              wchar_t dest[100] = L"";
              /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
              SNPRINTF(dest, 100-1, data);
              printWLine(dest);
          }
      }
      else
      {
          {
              wchar_t dest[100] = L"";
              /* FIX: Specify the format disallowing a format string vulnerability */
              SNPRINTF(dest, 100-1, L"%s", data);
              printWLine(dest);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G() - use badsource and goodsink by changing the first "if" so that
     both branches use the BadSource and the second "if" so that both branches
     use the GoodSink */
  static void goodB2G()
  {
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      if(globalReturnsTrueOrFalse())
      {
          {
              /* 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);
              }
          }
      }
      else
      {
          {
              /* 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);
              }
          }
      }
      if(globalReturnsTrueOrFalse())
      {
          {
              wchar_t dest[100] = L"";
              /* FIX: Specify the format disallowing a format string vulnerability */
              SNPRINTF(dest, 100-1, L"%s", data);
              printWLine(dest);
          }
      }
      else
      {
          {
              wchar_t dest[100] = L"";
              /* FIX: Specify the format disallowing a format string vulnerability */
              SNPRINTF(dest, 100-1, L"%s", data);
              printWLine(dest);
          }
      }
  }
  
  /* goodG2B() - use goodsource and badsink by changing the first "if" so that
     both branches use the GoodSource and the second "if" so that both branches
     use the BadSink */
  static void goodG2B()
  {
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      if(globalReturnsTrueOrFalse())
      {
          /* FIX: Use a fixed string that does not contain a format specifier */
          wcscpy(data, L"fixedstringtest");
      }
      else
      {
          /* FIX: Use a fixed string that does not contain a format specifier */
          wcscpy(data, L"fixedstringtest");
      }
      if(globalReturnsTrueOrFalse())
      {
          {
              wchar_t dest[100] = L"";
              /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
              SNPRINTF(dest, 100-1, data);
              printWLine(dest);
          }
      }
      else
      {
          {
              wchar_t dest[100] = L"";
              /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
              SNPRINTF(dest, 100-1, data);
              printWLine(dest);
          }
      }
  }
  
  void CWE134_Uncontrolled_Format_String__wchar_t_environment_snprintf_12_good()
  {
      goodB2G();
      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()...");
      CWE134_Uncontrolled_Format_String__wchar_t_environment_snprintf_12_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE134_Uncontrolled_Format_String__wchar_t_environment_snprintf_12_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  