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_01.c 2.6 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE401_Memory_Leak__char_calloc_01.c
  3. Label Definition File: CWE401_Memory_Leak.c.label.xml
  4. Template File: sources-sinks-01.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: 01 Baseline
  15. *
  16. * */
  17. #include "std_testcase.h"
  18. #include <wchar.h>
  19. #ifndef OMITBAD
  20. void CWE401_Memory_Leak__char_calloc_01_bad()
  21. {
  22. char * data;
  23. data = NULL;
  24. /* POTENTIAL FLAW: Allocate memory on the heap */
  25. data = (char *)calloc(100, sizeof(char));
  26. if (data == NULL) {exit(-1);}
  27. /* Initialize and make use of data */
  28. strcpy(data, "A String");
  29. printLine(data);
  30. /* POTENTIAL FLAW: No deallocation */
  31. ; /* empty statement needed for some flow variants */
  32. }
  33. #endif /* OMITBAD */
  34. #ifndef OMITGOOD
  35. /* goodG2B uses the GoodSource with the BadSink */
  36. static void goodG2B()
  37. {
  38. char * data;
  39. data = NULL;
  40. /* FIX: Use memory allocated on the stack with ALLOCA */
  41. data = (char *)ALLOCA(100*sizeof(char));
  42. /* Initialize and make use of data */
  43. strcpy(data, "A String");
  44. printLine(data);
  45. /* POTENTIAL FLAW: No deallocation */
  46. ; /* empty statement needed for some flow variants */
  47. }
  48. /* goodB2G uses the BadSource with the GoodSink */
  49. static void goodB2G()
  50. {
  51. char * data;
  52. data = NULL;
  53. /* POTENTIAL FLAW: Allocate memory on the heap */
  54. data = (char *)calloc(100, sizeof(char));
  55. if (data == NULL) {exit(-1);}
  56. /* Initialize and make use of data */
  57. strcpy(data, "A String");
  58. printLine(data);
  59. /* FIX: Deallocate memory */
  60. free(data);
  61. }
  62. void CWE401_Memory_Leak__char_calloc_01_good()
  63. {
  64. goodG2B();
  65. goodB2G();
  66. }
  67. #endif /* OMITGOOD */
  68. /* Below is the main(). It is only used when building this testcase on
  69. its own for testing or for building a binary to use in testing binary
  70. analysis tools. It is not used when compiling all the testcases as one
  71. application, which is how source code analysis tools are tested. */
  72. #ifdef INCLUDEMAIN
  73. int main(int argc, char * argv[])
  74. {
  75. /* seed randomness */
  76. srand( (unsigned)time(NULL) );
  77. #ifndef OMITGOOD
  78. printLine("Calling good()...");
  79. CWE401_Memory_Leak__char_calloc_01_good();
  80. printLine("Finished good()");
  81. #endif /* OMITGOOD */
  82. #ifndef OMITBAD
  83. printLine("Calling bad()...");
  84. CWE401_Memory_Leak__char_calloc_01_bad();
  85. printLine("Finished bad()");
  86. #endif /* OMITBAD */
  87. return 0;
  88. }
  89. #endif

No Description

Contributors (1)