text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__wchar_t_console_snprintf_65a.c
  Label Definition File: CWE134_Uncontrolled_Format_String.label.xml
  Template File: sources-sinks-65a.tmpl.c
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: console Read input from the console
   * GoodSource: Copy a fixed string into data
   * Sinks: snprintf
   *    GoodSink: snwprintf with "%s" as the third argument and data as the fourth
   *    BadSink : snwprintf with data as the third argument
   * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #ifdef _WIN32
  #define SNPRINTF _snwprintf
  #else
  #define SNPRINTF snprintf
  #endif
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void CWE134_Uncontrolled_Format_String__wchar_t_console_snprintf_65b_badSink(wchar_t * data);
  
  void CWE134_Uncontrolled_Format_String__wchar_t_console_snprintf_65_bad()
  {
      wchar_t * data;
      /* define a function pointer */
      void (*funcPtr) (wchar_t *) = CWE134_Uncontrolled_Format_String__wchar_t_console_snprintf_65b_badSink;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      {
          /* 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';
              }
          }
      }
      /* use the function pointer */
      funcPtr(data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE134_Uncontrolled_Format_String__wchar_t_console_snprintf_65b_goodG2BSink(wchar_t * data);
  
  static void goodG2B()
  {
      wchar_t * data;
      void (*funcPtr) (wchar_t *) = CWE134_Uncontrolled_Format_String__wchar_t_console_snprintf_65b_goodG2BSink;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      /* FIX: Use a fixed string that does not contain a format specifier */
      wcscpy(data, L"fixedstringtest");
      funcPtr(data);
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE134_Uncontrolled_Format_String__wchar_t_console_snprintf_65b_goodB2GSink(wchar_t * data);
  
  static void goodB2G()
  {
      wchar_t * data;
      void (*funcPtr) (wchar_t *) = CWE134_Uncontrolled_Format_String__wchar_t_console_snprintf_65b_goodB2GSink;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      {
          /* 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';
              }
          }
      }
      funcPtr(data);
  }
  
  void CWE134_Uncontrolled_Format_String__wchar_t_console_snprintf_65_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_snprintf_65_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE134_Uncontrolled_Format_String__wchar_t_console_snprintf_65_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  