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_environment_01.c 4.6 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE606_Unchecked_Loop_Condition__char_environment_01.c
  3. Label Definition File: CWE606_Unchecked_Loop_Condition.label.xml
  4. Template File: sources-sinks-01.tmpl.c
  5. */
  6. /*
  7. * @description
  8. * CWE: 606 Unchecked Input For Loop Condition
  9. * BadSource: environment Read input from an environment variable
  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: 01 Baseline
  15. *
  16. * */
  17. #include "std_testcase.h"
  18. #define MAX_LOOP 10000
  19. #ifndef _WIN32
  20. #include <wchar.h>
  21. #endif
  22. #define ENV_VARIABLE "ADD"
  23. #ifdef _WIN32
  24. #define GETENV getenv
  25. #else
  26. #define GETENV getenv
  27. #endif
  28. #ifndef OMITBAD
  29. void CWE606_Unchecked_Loop_Condition__char_environment_01_bad()
  30. {
  31. char * data;
  32. char dataBuffer[100] = "";
  33. data = dataBuffer;
  34. {
  35. /* Append input from an environment variable to data */
  36. size_t dataLen = strlen(data);
  37. char * environment = GETENV(ENV_VARIABLE);
  38. /* If there is data in the environment variable */
  39. if (environment != NULL)
  40. {
  41. /* POTENTIAL FLAW: Read data from an environment variable */
  42. strncat(data+dataLen, environment, 100-dataLen-1);
  43. }
  44. }
  45. {
  46. int i, n, intVariable;
  47. if (sscanf(data, "%d", &n) == 1)
  48. {
  49. /* POTENTIAL FLAW: user-supplied value 'n' could lead to very large loop iteration */
  50. intVariable = 0;
  51. for (i = 0; i < n; i++)
  52. {
  53. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  54. intVariable++; /* avoid a dead/empty code block issue */
  55. }
  56. printIntLine(intVariable);
  57. }
  58. }
  59. }
  60. #endif /* OMITBAD */
  61. #ifndef OMITGOOD
  62. /* goodG2B uses the GoodSource with the BadSink */
  63. static void goodG2B()
  64. {
  65. char * data;
  66. char dataBuffer[100] = "";
  67. data = dataBuffer;
  68. /* FIX: Set data to a number less than MAX_LOOP */
  69. strcpy(data, "15");
  70. {
  71. int i, n, intVariable;
  72. if (sscanf(data, "%d", &n) == 1)
  73. {
  74. /* POTENTIAL FLAW: user-supplied value 'n' could lead to very large loop iteration */
  75. intVariable = 0;
  76. for (i = 0; i < n; i++)
  77. {
  78. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  79. intVariable++; /* avoid a dead/empty code block issue */
  80. }
  81. printIntLine(intVariable);
  82. }
  83. }
  84. }
  85. /* goodB2G uses the BadSource with the GoodSink */
  86. static void goodB2G()
  87. {
  88. char * data;
  89. char dataBuffer[100] = "";
  90. data = dataBuffer;
  91. {
  92. /* Append input from an environment variable to data */
  93. size_t dataLen = strlen(data);
  94. char * environment = GETENV(ENV_VARIABLE);
  95. /* If there is data in the environment variable */
  96. if (environment != NULL)
  97. {
  98. /* POTENTIAL FLAW: Read data from an environment variable */
  99. strncat(data+dataLen, environment, 100-dataLen-1);
  100. }
  101. }
  102. {
  103. int i, n, intVariable;
  104. if (sscanf(data, "%d", &n) == 1)
  105. {
  106. /* FIX: limit loop iteration counts */
  107. if (n < MAX_LOOP)
  108. {
  109. intVariable = 0;
  110. for (i = 0; i < n; i++)
  111. {
  112. /* INCIDENTAL: CWE 561: Dead Code - non-avoidable if n <= 0 */
  113. intVariable++; /* avoid a dead/empty code block issue */
  114. }
  115. printIntLine(intVariable);
  116. }
  117. }
  118. }
  119. }
  120. void CWE606_Unchecked_Loop_Condition__char_environment_01_good()
  121. {
  122. goodG2B();
  123. goodB2G();
  124. }
  125. #endif /* OMITGOOD */
  126. /* Below is the main(). It is only used when building this testcase on
  127. its own for testing or for building a binary to use in testing binary
  128. analysis tools. It is not used when compiling all the testcases as one
  129. application, which is how source code analysis tools are tested. */
  130. #ifdef INCLUDEMAIN
  131. int main(int argc, char * argv[])
  132. {
  133. /* seed randomness */
  134. srand( (unsigned)time(NULL) );
  135. #ifndef OMITGOOD
  136. printLine("Calling good()...");
  137. CWE606_Unchecked_Loop_Condition__char_environment_01_good();
  138. printLine("Finished good()");
  139. #endif /* OMITGOOD */
  140. #ifndef OMITBAD
  141. printLine("Calling bad()...");
  142. CWE606_Unchecked_Loop_Condition__char_environment_01_bad();
  143. printLine("Finished bad()");
  144. #endif /* OMITBAD */
  145. return 0;
  146. }
  147. #endif

No Description

Contributors (1)