Skip to content
Snippets Groups Projects
Commit fa56d8d8 authored by tophyan1's avatar tophyan1
Browse files

Fix memory leak

parent cbc75e86
No related branches found
No related tags found
No related merge requests found
......@@ -98,11 +98,13 @@ Tree* insert(char* command, Tree* tree) {
if (2 != sscanf(command, "i %d %19s", &age, name)){
fprintf(stderr, "Failed to parse insert command: not enough parameters filled\n");
free(name);
return tree;
}
if (!validate_name(name, 20)){
fprintf(stderr, "Failed to insert: invalid name\n");
free(name);
return tree;
}
......@@ -112,10 +114,12 @@ Tree* insert(char* command, Tree* tree) {
if (tree_find(tree, age, name)) {
fprintf(stderr, "Failed to insert: item already exists\n");
free(name);
return tree;
}
tree_insert(tree, age, name);
free(name);
return tree;
}
......@@ -126,8 +130,11 @@ void erase(char* command, Tree* tree) {
if (2 != sscanf(command, "e %d %19s", &age, name)){
fprintf(stderr, "Failed to parse erase command: not enough parameters filled\n");
free(name);
return;
}
tree_erase(tree, age, name);
free(name);
}
// You are allowed to change anything about this function to fix it
......@@ -140,6 +147,7 @@ void check(char* command, Tree* tree) {
}
Node* result = tree_find(tree, age, name);
free(name);
if (result){
printf("y\n");
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment