You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CWE606_Unchecked_Loop_Condition__char_file_22b.c 4.6 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE606_Unchecked_Loop_Condition__char_file_22b.c
  3. Label Definition File: CWE606_Unchecked_Loop_Condition.label.xml
  4. Template File: sources-sinks-22b.tmpl.c
  5. */
  6. /*
  7. * @description
  8. * CWE: 606 Unchecked Input For Loop Condition
  9. * BadSource: file Read input from a file
  10. * GoodSource: Input a number less than MAX_LOOP
  11. * Sinks:
  12. * GoodSink: Use data as the for loop variant after checking to see if it is less than MAX_LOOP
  13. * BadSink : Use data as the for loop variant without checking its size
  14. * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.
  15. *
  16. * */
  17. #include "std_testcase.h"
  18. #define MAX_LOOP 10000
  19. #ifndef _WIN32
  20. #include <wchar.h>
  21. #endif
  22. #ifndef OMITBAD
  23. /* The global variable below is used to drive control flow in the sink function */
  24. extern int CWE606_Unchecked_Loop_Condition__char_file_22_badGlobal;
  25. void CWE606_Unchecked_Loop_Condition__char_file_22_badSink(char * data)
  26. {
  27. if(CWE606_Unchecked_Loop_Condition__char_file_22_badGlobal)
  28. {
  29. {
  30. int i, n, intVariable;
  31. if (sscanf(data, "%d", &n) == 1)
  32. {
  33. /* POTENTIAL FLAW: user-supplied value 'n' could lead to very large loop iteration */
  34. intVariable = 0;
  35. for (i = 0; i < n; i++)
  36. {
  37. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  38. intVariable++; /* avoid a dead/empty code block issue */
  39. }
  40. printIntLine(intVariable);
  41. }
  42. }
  43. }
  44. }
  45. #endif /* OMITBAD */
  46. #ifndef OMITGOOD
  47. /* The global variables below are used to drive control flow in the sink functions. */
  48. extern int CWE606_Unchecked_Loop_Condition__char_file_22_goodB2G1Global;
  49. extern int CWE606_Unchecked_Loop_Condition__char_file_22_goodB2G2Global;
  50. extern int CWE606_Unchecked_Loop_Condition__char_file_22_goodG2BGlobal;
  51. /* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */
  52. void CWE606_Unchecked_Loop_Condition__char_file_22_goodB2G1Sink(char * data)
  53. {
  54. if(CWE606_Unchecked_Loop_Condition__char_file_22_goodB2G1Global)
  55. {
  56. /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
  57. printLine("Benign, fixed string");
  58. }
  59. else
  60. {
  61. {
  62. int i, n, intVariable;
  63. if (sscanf(data, "%d", &n) == 1)
  64. {
  65. /* FIX: limit loop iteration counts */
  66. if (n < MAX_LOOP)
  67. {
  68. intVariable = 0;
  69. for (i = 0; i < n; i++)
  70. {
  71. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  72. intVariable++; /* avoid a dead/empty code block issue */
  73. }
  74. printIntLine(intVariable);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. /* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */
  81. void CWE606_Unchecked_Loop_Condition__char_file_22_goodB2G2Sink(char * data)
  82. {
  83. if(CWE606_Unchecked_Loop_Condition__char_file_22_goodB2G2Global)
  84. {
  85. {
  86. int i, n, intVariable;
  87. if (sscanf(data, "%d", &n) == 1)
  88. {
  89. /* FIX: limit loop iteration counts */
  90. if (n < MAX_LOOP)
  91. {
  92. intVariable = 0;
  93. for (i = 0; i < n; i++)
  94. {
  95. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  96. intVariable++; /* avoid a dead/empty code block issue */
  97. }
  98. printIntLine(intVariable);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. /* goodG2B() - use goodsource and badsink */
  105. void CWE606_Unchecked_Loop_Condition__char_file_22_goodG2BSink(char * data)
  106. {
  107. if(CWE606_Unchecked_Loop_Condition__char_file_22_goodG2BGlobal)
  108. {
  109. {
  110. int i, n, intVariable;
  111. if (sscanf(data, "%d", &n) == 1)
  112. {
  113. /* POTENTIAL FLAW: user-supplied value 'n' could lead to very large loop iteration */
  114. intVariable = 0;
  115. for (i = 0; i < n; i++)
  116. {
  117. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  118. intVariable++; /* avoid a dead/empty code block issue */
  119. }
  120. printIntLine(intVariable);
  121. }
  122. }
  123. }
  124. }
  125. #endif /* OMITGOOD */

No Description

Contributors (1)