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.

CWE401_Memory_Leak__char_calloc_02.c 4.2 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE401_Memory_Leak__char_calloc_02.c
  3. Label Definition File: CWE401_Memory_Leak.c.label.xml
  4. Template File: sources-sinks-02.tmpl.c
  5. */
  6. /*
  7. * @description
  8. * CWE: 401 Memory Leak
  9. * BadSource: calloc Allocate data using calloc()
  10. * GoodSource: Allocate data on the stack
  11. * Sinks:
  12. * GoodSink: call free() on data
  13. * BadSink : no deallocation of data
  14. * Flow Variant: 02 Control flow: if(1) and if(0)
  15. *
  16. * */
  17. #include "std_testcase.h"
  18. #include <wchar.h>
  19. #ifndef OMITBAD
  20. void CWE401_Memory_Leak__char_calloc_02_bad()
  21. {
  22. char * data;
  23. data = NULL;
  24. if(1)
  25. {
  26. /* POTENTIAL FLAW: Allocate memory on the heap */
  27. data = (char *)calloc(100, sizeof(char));
  28. if (data == NULL) {exit(-1);}
  29. /* Initialize and make use of data */
  30. strcpy(data, "A String");
  31. printLine(data);
  32. }
  33. if(1)
  34. {
  35. /* POTENTIAL FLAW: No deallocation */
  36. ; /* empty statement needed for some flow variants */
  37. }
  38. }
  39. #endif /* OMITBAD */
  40. #ifndef OMITGOOD
  41. /* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */
  42. static void goodB2G1()
  43. {
  44. char * data;
  45. data = NULL;
  46. if(1)
  47. {
  48. /* POTENTIAL FLAW: Allocate memory on the heap */
  49. data = (char *)calloc(100, sizeof(char));
  50. if (data == NULL) {exit(-1);}
  51. /* Initialize and make use of data */
  52. strcpy(data, "A String");
  53. printLine(data);
  54. }
  55. if(0)
  56. {
  57. /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
  58. printLine("Benign, fixed string");
  59. }
  60. else
  61. {
  62. /* FIX: Deallocate memory */
  63. free(data);
  64. }
  65. }
  66. /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  67. static void goodB2G2()
  68. {
  69. char * data;
  70. data = NULL;
  71. if(1)
  72. {
  73. /* POTENTIAL FLAW: Allocate memory on the heap */
  74. data = (char *)calloc(100, sizeof(char));
  75. if (data == NULL) {exit(-1);}
  76. /* Initialize and make use of data */
  77. strcpy(data, "A String");
  78. printLine(data);
  79. }
  80. if(1)
  81. {
  82. /* FIX: Deallocate memory */
  83. free(data);
  84. }
  85. }
  86. /* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */
  87. static void goodG2B1()
  88. {
  89. char * data;
  90. data = NULL;
  91. if(0)
  92. {
  93. /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
  94. printLine("Benign, fixed string");
  95. }
  96. else
  97. {
  98. /* FIX: Use memory allocated on the stack with ALLOCA */
  99. data = (char *)ALLOCA(100*sizeof(char));
  100. /* Initialize and make use of data */
  101. strcpy(data, "A String");
  102. printLine(data);
  103. }
  104. if(1)
  105. {
  106. /* POTENTIAL FLAW: No deallocation */
  107. ; /* empty statement needed for some flow variants */
  108. }
  109. }
  110. /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  111. static void goodG2B2()
  112. {
  113. char * data;
  114. data = NULL;
  115. if(1)
  116. {
  117. /* FIX: Use memory allocated on the stack with ALLOCA */
  118. data = (char *)ALLOCA(100*sizeof(char));
  119. /* Initialize and make use of data */
  120. strcpy(data, "A String");
  121. printLine(data);
  122. }
  123. if(1)
  124. {
  125. /* POTENTIAL FLAW: No deallocation */
  126. ; /* empty statement needed for some flow variants */
  127. }
  128. }
  129. void CWE401_Memory_Leak__char_calloc_02_good()
  130. {
  131. goodB2G1();
  132. goodB2G2();
  133. goodG2B1();
  134. goodG2B2();
  135. }
  136. #endif /* OMITGOOD */
  137. /* Below is the main(). It is only used when building this testcase on
  138. its own for testing or for building a binary to use in testing binary
  139. analysis tools. It is not used when compiling all the testcases as one
  140. application, which is how source code analysis tools are tested. */
  141. #ifdef INCLUDEMAIN
  142. int main(int argc, char * argv[])
  143. {
  144. /* seed randomness */
  145. srand( (unsigned)time(NULL) );
  146. #ifndef OMITGOOD
  147. printLine("Calling good()...");
  148. CWE401_Memory_Leak__char_calloc_02_good();
  149. printLine("Finished good()");
  150. #endif /* OMITGOOD */
  151. #ifndef OMITBAD
  152. printLine("Calling bad()...");
  153. CWE401_Memory_Leak__char_calloc_02_bad();
  154. printLine("Finished bad()");
  155. #endif /* OMITBAD */
  156. return 0;
  157. }
  158. #endif

No Description

Contributors (1)