-
Notifications
You must be signed in to change notification settings - Fork 6
/
double_free.cpp
65 lines (52 loc) · 1.57 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
namespace DF_01 {
class FoobarClass {
public:
FoobarClass(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';
}
}
~FoobarClass() { delete[] data; }
void printData() { printf("%s\n", data); }
FoobarClass &operator=(const FoobarClass &otherxClassObject) {
if (&otherxClassObject != this) {
this->data = new char[strlen(otherxClassObject.data) + 1];
strcpy(this->data, otherxClassObject.data);
}
return *this;
}
private:
char *data;
};
void otherx() {
FoobarClass otherxClassObject("One");
/* FLAW: There is no copy constructor in the class - this will cause a double
* free in the destructor */
FoobarClass otherxClassObjectCopy(otherxClassObject);
otherxClassObjectCopy.printData();
}
} // namespace DF_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 DF_01; /* so that we can use othery
and otherx easily */
int main(int argc, char *argv[]) {
/* seed randomness */
srand((unsigned)time(NULL));
printf("Calling otherx()...");
otherx();
printf("Finished otherx()");
return 0;
}