text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE606_Unchecked_Loop_Condition__char_file_74b.cpp
  Label Definition File: CWE606_Unchecked_Loop_Condition.label.xml
  Template File: sources-sinks-74b.tmpl.cpp
  */
  /*
   * @description
   * CWE: 606 Unchecked Input For Loop Condition
   * BadSource: file Read input from a file
   * 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: 74 Data flow: data passed in a map from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  #include <map>
  
  #define MAX_LOOP 10000
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  using namespace std;
  
  namespace CWE606_Unchecked_Loop_Condition__char_file_74
  {
  
  #ifndef OMITBAD
  
  void badSink(map<int, char *> dataMap)
  {
      /* copy data out of dataMap */
      char * data = dataMap[2];
      {
          int i, n, intVariable;
          if (sscanf(data, "%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 */
  void goodG2BSink(map<int, char *> dataMap)
  {
      char * data = dataMap[2];
      {
          int i, n, intVariable;
          if (sscanf(data, "%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 */
  void goodB2GSink(map<int, char *> dataMap)
  {
      char * data = dataMap[2];
      {
          int i, n, intVariable;
          if (sscanf(data, "%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);
              }
          }
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  