text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE606_Unchecked_Loop_Condition__wchar_t_console_01.c
  Label Definition File: CWE606_Unchecked_Loop_Condition.label.xml
  Template File: sources-sinks-01.tmpl.c
  */
  /*
   * @description
   * CWE: 606 Unchecked Input For Loop Condition
   * BadSource: console Read input from the console
   * GoodSource: Input a number less than MAX_LOOP
   * Sinks:
   *    GoodSink: Use data as the for loop variant after checking to see if it is less than MAX_LOOP
   *    BadSink : Use data as the for loop variant without checking its size
   * Flow Variant: 01 Baseline
   *
   * */
  
  #include "std_testcase.h"
  
  #define MAX_LOOP 10000
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #ifndef OMITBAD
  
  void CWE606_Unchecked_Loop_Condition__wchar_t_console_01_bad()
  {
      wchar_t * data;
      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';
              }
          }
      }
      {
          int i, n, intVariable;
          if (swscanf(data, L"%d", &n) == 1)
          {
              /* POTENTIAL FLAW: user-supplied value 'n' could lead to very large loop iteration */
              intVariable = 0;
              for (i = 0; i < n; i++)
              {
                  /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
                  intVariable++; /* avoid a dead/empty code block issue */
              }
              printIntLine(intVariable);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  static void goodG2B()
  {
      wchar_t * data;
      wchar_t dataBuffer[100] = L"";
      data = dataBuffer;
      /* FIX: Set data to a number less than MAX_LOOP */
      wcscpy(data, L"15");
      {
          int i, n, intVariable;
          if (swscanf(data, L"%d", &n) == 1)
          {
              /* POTENTIAL FLAW: user-supplied value 'n' could lead to very large loop iteration */
              intVariable = 0;
              for (i = 0; i < n; i++)
              {
                  /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
                  intVariable++; /* avoid a dead/empty code block issue */
              }
              printIntLine(intVariable);
          }
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  static void goodB2G()
  {
      wchar_t * data;
      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';
              }
          }
      }
      {
          int i, n, intVariable;
          if (swscanf(data, L"%d", &n) == 1)
          {
              /* FIX: limit loop iteration counts */
              if (n < MAX_LOOP)
              {
                  intVariable = 0;
                  for (i = 0; i < n; i++)
                  {
                      /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
                      intVariable++; /* avoid a dead/empty code block issue */
                  }
                  printIntLine(intVariable);
              }
          }
      }
  }
  
  void CWE606_Unchecked_Loop_Condition__wchar_t_console_01_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()...");
      CWE606_Unchecked_Loop_Condition__wchar_t_console_01_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE606_Unchecked_Loop_Condition__wchar_t_console_01_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  