-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
128 lines (101 loc) · 2.94 KB
/
main.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Copyright (C) 2018 Shyamsundar.R
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef unsigned int uint32_t;
struct mem_header {
uint32_t magic;
size_t size;
};
#define GF_MEM_HEADER_SIZE (sizeof(struct mem_header))
#define GF_MEM_TRAILER_SIZE 8
#define GF_MEM_HEADER_MAGIC 0xCAFEBABE
#define GF_MEM_TRAILER_MAGIC 0xBAADF00D
void *
my_malloc(size_t size)
{
size_t tot_size = 0;
char *ptr = NULL;
struct mem_header *header = NULL;
tot_size = size + GF_MEM_HEADER_SIZE + GF_MEM_TRAILER_SIZE;
ptr = malloc(tot_size);
if (!ptr) {
return NULL;
}
header = (struct mem_header *)ptr;
header->size = size;
header->magic = GF_MEM_HEADER_MAGIC;
ptr += sizeof(struct mem_header);
*(uint32_t *)(ptr + size) = GF_MEM_TRAILER_MAGIC;
return (void *)ptr;
}
void
my_free(void *free_ptr)
{
void *ptr = NULL;
struct mem_header *header = NULL;
if (!free_ptr)
return;
ptr = free_ptr - GF_MEM_HEADER_SIZE;
header = (struct mem_header *)ptr;
if (!(GF_MEM_HEADER_MAGIC == header->magic)) {
printf ("Header corrupted\n");
} else {
printf ("Header intact!\n");
}
if (!(GF_MEM_TRAILER_MAGIC ==
*(uint32_t *)((char *)free_ptr + header->size))) {
printf ("Trailer corrupted\n");
} else {
printf ("Trailer intact!\n");
}
free(ptr);
}
#define COPY_STR "Test string!"
#define TEST_FILE "./test.txt"
void
main(void) {
char *buf, *taint_buf;
int fd;
ssize_t bytesread;
buf = my_malloc(sizeof(COPY_STR));
if (buf != NULL) {
memcpy(buf, COPY_STR, sizeof(COPY_STR));
printf("%s\n", buf);
my_free(buf);
}
taint_buf = my_malloc(sizeof(COPY_STR));
if (taint_buf == NULL)
return;
fd = open(TEST_FILE, O_RDWR);
if (fd == -1) {
printf ("Missing test file %s\n", TEST_FILE);
my_free(taint_buf);
return;
}
bytesread = read(fd, taint_buf, sizeof(COPY_STR));
if (bytesread == sizeof(COPY_STR)) {
/*taint_buf[bytesread - 1] = '\0';*/
printf("%s\n", taint_buf);
} else {
printf ("Not enough data in test file %s\n", TEST_FILE);
}
my_free (taint_buf);
return;
}