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.

CWE415_Double_Free__malloc_free_char_02.c 4.2 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE415_Double_Free__malloc_free_char_02.c
  3. Label Definition File: CWE415_Double_Free__malloc_free.label.xml
  4. Template File: sources-sinks-02.tmpl.c
  5. */
  6. /*
  7. * @description
  8. * CWE: 415 Double Free
  9. * BadSource: Allocate data using malloc() and Deallocate data using free()
  10. * GoodSource: Allocate data using malloc()
  11. * Sinks:
  12. * GoodSink: do nothing
  13. * BadSink : Deallocate data using free()
  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 CWE415_Double_Free__malloc_free_char_02_bad()
  21. {
  22. char * data;
  23. /* Initialize data */
  24. data = NULL;
  25. if(1)
  26. {
  27. data = (char *)malloc(100*sizeof(char));
  28. if (data == NULL) {exit(-1);}
  29. /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */
  30. free(data);
  31. }
  32. if(1)
  33. {
  34. /* POTENTIAL FLAW: Possibly freeing memory twice */
  35. free(data);
  36. }
  37. }
  38. #endif /* OMITBAD */
  39. #ifndef OMITGOOD
  40. /* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */
  41. static void goodB2G1()
  42. {
  43. char * data;
  44. /* Initialize data */
  45. data = NULL;
  46. if(1)
  47. {
  48. data = (char *)malloc(100*sizeof(char));
  49. if (data == NULL) {exit(-1);}
  50. /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */
  51. free(data);
  52. }
  53. if(0)
  54. {
  55. /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
  56. printLine("Benign, fixed string");
  57. }
  58. else
  59. {
  60. /* do nothing */
  61. /* FIX: Don't attempt to free the memory */
  62. ; /* empty statement needed for some flow variants */
  63. }
  64. }
  65. /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  66. static void goodB2G2()
  67. {
  68. char * data;
  69. /* Initialize data */
  70. data = NULL;
  71. if(1)
  72. {
  73. data = (char *)malloc(100*sizeof(char));
  74. if (data == NULL) {exit(-1);}
  75. /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */
  76. free(data);
  77. }
  78. if(1)
  79. {
  80. /* do nothing */
  81. /* FIX: Don't attempt to free the memory */
  82. ; /* empty statement needed for some flow variants */
  83. }
  84. }
  85. /* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */
  86. static void goodG2B1()
  87. {
  88. char * data;
  89. /* Initialize 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. data = (char *)malloc(100*sizeof(char));
  99. if (data == NULL) {exit(-1);}
  100. /* FIX: Do NOT free data in the source - the bad sink frees data */
  101. }
  102. if(1)
  103. {
  104. /* POTENTIAL FLAW: Possibly freeing memory twice */
  105. free(data);
  106. }
  107. }
  108. /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  109. static void goodG2B2()
  110. {
  111. char * data;
  112. /* Initialize data */
  113. data = NULL;
  114. if(1)
  115. {
  116. data = (char *)malloc(100*sizeof(char));
  117. if (data == NULL) {exit(-1);}
  118. /* FIX: Do NOT free data in the source - the bad sink frees data */
  119. }
  120. if(1)
  121. {
  122. /* POTENTIAL FLAW: Possibly freeing memory twice */
  123. free(data);
  124. }
  125. }
  126. void CWE415_Double_Free__malloc_free_char_02_good()
  127. {
  128. goodB2G1();
  129. goodB2G2();
  130. goodG2B1();
  131. goodG2B2();
  132. }
  133. #endif /* OMITGOOD */
  134. /* Below is the main(). It is only used when building this testcase on
  135. its own for testing or for building a binary to use in testing binary
  136. analysis tools. It is not used when compiling all the testcases as one
  137. application, which is how source code analysis tools are tested. */
  138. #ifdef INCLUDEMAIN
  139. int main(int argc, char * argv[])
  140. {
  141. /* seed randomness */
  142. srand( (unsigned)time(NULL) );
  143. #ifndef OMITGOOD
  144. printLine("Calling good()...");
  145. CWE415_Double_Free__malloc_free_char_02_good();
  146. printLine("Finished good()");
  147. #endif /* OMITGOOD */
  148. #ifndef OMITBAD
  149. printLine("Calling bad()...");
  150. CWE415_Double_Free__malloc_free_char_02_bad();
  151. printLine("Finished bad()");
  152. #endif /* OMITBAD */
  153. return 0;
  154. }
  155. #endif

No Description

Contributors (1)