forked from vanhauser-thc/vulntest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
double_free.cpp
69 lines (56 loc) · 1.64 KB
/
double_free.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* @description Double Free
*
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
namespace CWE415_Double_Free__no_copy_const_01 {
class BadClass {
public:
BadClass(const char *data) {
if (data) {
this->data = new char[strlen(data) + 1];
strcpy(this->data, data);
} else {
this->data = new char[1];
*(this->data) = '\0';
}
}
~BadClass() { delete[] data; }
void printData() { printf("%s\n", data); }
BadClass &operator=(const BadClass &badClassObject) {
if (&badClassObject != this) {
this->data = new char[strlen(badClassObject.data) + 1];
strcpy(this->data, badClassObject.data);
}
return *this;
}
private:
char *data;
};
void bad() {
BadClass badClassObject("One");
/* FLAW: There is no copy constructor in the class - this will cause a double
* free in the destructor */
BadClass badClassObjectCopy(badClassObject);
badClassObjectCopy.printData();
}
} // namespace CWE415_Double_Free__no_copy_const_01
/* Below is the main(). It is only used when building this testcase on
* its own for testing or for building a binary to use in testing binary
* analysis tools. It is not used when compiling all the testcases as one
* application, which is how source code analysis tools are tested.
*/
using namespace CWE415_Double_Free__no_copy_const_01; /* so that we can use good
and bad easily */
int main(int argc, char *argv[]) {
/* seed randomness */
srand((unsigned)time(NULL));
printf("Calling bad()...");
bad();
printf("Finished bad()");
return 0;
}