I am having an issue figuring out what wrong with my code below. I run the full code and I do a lot of input testing and errors are being handled as I want them to. I also use stuffs like valgrind, cppchecker to check for bugs which I fixed the bugs. I then decided to use afl-fuzzer to do an advanced bug detection on my codes and then I get a lot of crashes due to the below line of code. However, most of the crashes are due to segmentation fault. but I don't seem to see what is wrong with the code. Any help will be appreciated. Below is the function that keeps giving the error. Which I think has to do with sscanf
:
Tree* insert(char* command, Tree* tree) {
int age;
char* name = malloc(sizeof(char) * 20);
if (2 != sscanf(command, "i %d %20s", &age, name)){
fprintf(stderr, "Failed to parse insert command: not enough parameters filled\n");
// return NULL;
}
if (tree == NULL){
tree = tree_create();
}
tree_insert(tree, age, name);
return tree;
}
tree_create function
Tree* tree_create(){
Tree *tree = malloc(sizeof(Tree));
tree->root = NULL;
return tree;
}
tree_insert
void tree_insert(Tree* tree, int age, char* name) {
if (tree->root == NULL) {
Node *node = calloc(1, sizeof(Node));
node->name = name;
node->age = age;
node->isRoot = true;
node->right = NULL;
node->left = NULL;
tree->root = node;
} else {
node_insert(tree->root, age, name, 1);
}
}