text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__wchar_t_console_w32_vsnprintf_17.c
  Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml
  Template File: sources-vasinks-17.tmpl.c
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: console Read input from the console
   * GoodSource: Copy a fixed string into data
   * Sinks: w32_vsnprintf
   *    GoodSink: _vsnwprintf with a format string
   *    BadSink : _vsnwprintf without a format string
   * Flow Variant: 17 Control flow: for loops
   *
   * */
  
  #include <stdarg.h>
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #ifndef OMITBAD
  
  static void badVaSinkB(wchar_t * data, ...)
  {
      {
          wchar_t dest[100] = L"";
          va_list args;
          va_start(args, data);
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          _vsnwprintf(dest, 100-1, data, args);
          va_end(args);
          printWLine(dest);
      }
  }
  
  void CWE134_Uncontrolled_Format_String__wchar_t_console_w32_vsnprintf_17_bad()
  {
      int i,j;
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      for(i = 0; i < 1; i++)
      {
          {
              /* Read input from the console */
              size_t dataLen = wcslen(data);
              /* if there is room in data, read into it from the console */
              if (100-dataLen > 1)
              {
                  /* POTENTIAL FLAW: Read data from the console */
                  if (fgetws(data+dataLen, (int)(100-dataLen), stdin) != NULL)
                  {
                      /* The next few lines remove the carriage return from the string that is
                       * inserted by fgetws() */
                      dataLen = wcslen(data);
                      if (dataLen > 0 && data[dataLen-1] == L'\n')
                      {
                          data[dataLen-1] = L'\0';
                      }
                  }
                  else
                  {
                      printLine("fgetws() failed");
                      /* Restore NUL terminator if fgetws fails */
                      data[dataLen] = L'\0';
                  }
              }
          }
      }
      for(j = 0; j < 1; j++)
      {
          badVaSinkB(data, data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  static void goodB2GVaSinkG(wchar_t * data, ...)
  {
      {
          wchar_t dest[100] = L"";
          va_list args;
          va_start(args, data);
          /* FIX: Specify the format disallowing a format string vulnerability */
          _vsnwprintf(dest, 100-1, L"%s", args);
          va_end(args);
          printWLine(dest);
      }
  }
  
  /* goodB2G() - use badsource and goodsink in the for statements */
  static void goodB2G()
  {
      int i,k;
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      for(i = 0; i < 1; i++)
      {
          {
              /* Read input from the console */
              size_t dataLen = wcslen(data);
              /* if there is room in data, read into it from the console */
              if (100-dataLen > 1)
              {
                  /* POTENTIAL FLAW: Read data from the console */
                  if (fgetws(data+dataLen, (int)(100-dataLen), stdin) != NULL)
                  {
                      /* The next few lines remove the carriage return from the string that is
                       * inserted by fgetws() */
                      dataLen = wcslen(data);
                      if (dataLen > 0 && data[dataLen-1] == L'\n')
                      {
                          data[dataLen-1] = L'\0';
                      }
                  }
                  else
                  {
                      printLine("fgetws() failed");
                      /* Restore NUL terminator if fgetws fails */
                      data[dataLen] = L'\0';
                  }
              }
          }
      }
      for(k = 0; k < 1; k++)
      {
          goodB2GVaSinkG(data, data);
      }
  }
  
  static void goodG2BVaSinkB(wchar_t * data, ...)
  {
      {
          wchar_t dest[100] = L"";
          va_list args;
          va_start(args, data);
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          _vsnwprintf(dest, 100-1, data, args);
          va_end(args);
          printWLine(dest);
      }
  }
  
  /* goodG2B() - use goodsource and badsink in the for statements */
  static void goodG2B()
  {
      int h,j;
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      for(h = 0; h < 1; h++)
      {
          /* FIX: Use a fixed string that does not contain a format specifier */
          wcscpy(data, L"fixedstringtest");
      }
      for(j = 0; j < 1; j++)
      {
          goodG2BVaSinkB(data, data);
      }
  }
  
  void CWE134_Uncontrolled_Format_String__wchar_t_console_w32_vsnprintf_17_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_console_w32_vsnprintf_17_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE134_Uncontrolled_Format_String__wchar_t_console_w32_vsnprintf_17_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  