heap题出题模板

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int *heap_list[16];

void init() {
setvbuf(stdout,0,2,0);
setvbuf(stderr,0,2,0);
setvbuf(stdin,0,2,0);
memset(heap_list, 0, sizeof(heap_list));
}

void menu() {
printf("1. Add a new note\n");
printf("2. Delete a note\n");
printf("3. Edit a note\n");
printf("4. Check the note\n");
printf("5. Exit\n");
puts("Your choice: ");
}

void add_note()
{
for(int i=0; i<16; i++)
{
if(!heap_list[i])
{
char size[8];
int size_num ;
char *note;
while(1)
{
printf("Size: ");
read(0, size, 4);
size_num = atoi(size);
note=(char *) malloc(size_num);
heap_list[i]=(int*)note;
if(size_num <=0)
{
puts("invalid size");
continue;
}
else
{
break;
}
// if(size_num > 0x80)
// {
// puts("Size is too big!");
// exit(1);
// }
}

// char buffer[size_num];
// memset(buffer, 0, sizeof(buffer));
if(!note)
{
puts("Memory allocation failed!");
exit(2);
}
printf("Content: ");
read(0, note, size_num);
printf("Note %d added successfully!\n",i);
// off by null
// strcpy(note, buffer);
// off by one
// read(0, buffer, size_num+1);
break;
}
else if(i >= 15)
{
puts("Heap is full!");
break;
}
}
}

void delete_note()
{
char buf[8];
int index_num;
printf("Index: ");
read(0, buf, 4);
index_num = atoi(buf);
if(index_num < 0 || index_num > 15)
{
puts("Invalid index");
return;
}
if(heap_list[index_num])
{
free(heap_list[index_num]);
heap_list[index_num] = NULL; // UAF
puts("Note deleted successfully!");
}
else
{
puts("Free corrupted!");
exit(3);
}
}

void edit_note()
{
puts("Please enter the index of the note you want to edit:");
char buf[8];
int index_num;
read(0, buf, 4);
index_num = atoi(buf);
if(index_num < 0 || index_num > 15)
{
puts("Invalid index");
return;
}
else
{
size_t current_size = malloc_usable_size(heap_list[index_num]);
puts("Please enter the new content:");
read(0, heap_list[index_num], current_size-1);
((char *)heap_list[index_num])[current_size-1] = '\0';
}
// 堆溢出
// else
// {
// puts("Please enter the length of item name:");
// char size[8];
// int size_num;
// read(0, size, 4);
// size_num = atoi(size);
// if (size_num <= 0)
// {
// puts("Invalid size");
// return;
// }
// puts("Please enter the new content:");
// read(0, heap_list[index_num], size_num);
// puts("Note edited successfully!");
// }

// 堆溢出2
// else
// {
// char ch;
// int offset = 0;
// puts("Please enter the new content:");
// while (1)
// {
// if (read(0, &ch, 1) <= 0)
// break;
// if (ch == '\n')
// break;
// ((char *)heap_list[index_num])[offset++] = ch;
// }
// puts("Note edited successfully!");
// }
// }
//
// off-by-one:
// else
// {
// puts("Please enter the new content:");
// read(0, heap_list[index_num], size_num+1);
// puts("Note edited successfully!");
// }

// off-by-null:
// else
// {
// puts("Please enter the new content:");
// read(0, heap_list[index_num], size_num);
// ((char *)heap_list[index_num])[size_num] = '\0';
// puts("Note edited successfully!");
}

void check_note()
{
puts("Please enter the index of the note you want to edit:");
char buf[8];
int index_num;
read(0, buf, 4);
index_num = atoi(buf);
if (index_num < 0 || index_num > 15)
{
puts("Invalid index");
return;
}
else
{
printf("Note %d: %s\n", index_num, (char *)heap_list[index_num]);
}
}



int main()
{
init();
char choice[8];
printf("Welcome to the note management system!\n");
printf("Gift %p", &puts);
while (1)
{
menu();
read(0, choice, 8);
switch (atoi(choice))
{
case 1:
add_note();
break;
case 2:
delete_note();
break;
case 3:
edit_note();
break;
case 4:
check_note();
break;
case 5:
puts("Bye!");
exit(0);
break;
default :
puts("Invalid choice");
break;
}
}
return 0;
}



heap题出题模板
http://example.com/2025/04/21/heap题出题模板/
发布于
2025年4月21日
许可协议