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.

CWE416_Use_After_Free__malloc_free_char_03.c 5.1 kB

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

No Description

Contributors (1)