-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect double free behavior #181
Comments
I think the main function contains a typo and it should be fixed as follows: int main()
{
hashmap_t *map = hashmap_create(16);
map->buckets[0] = malloc(sizeof(hashmap_node_t)); // Simulates put at first bucket
map->buckets[0]->key = calloc(1, sizeof(char));
- map->buckets[0]->key = malloc(sizeof(int));
+ map->buckets[0]->val = malloc(sizeof(int));
map->buckets[0]->next = NULL;
hashmap_free(map);
return 0;
} |
Thanks for the correction. I've edited the code. |
void hashmap_free(hashmap_t *map)
{
for (int i = 0; i < map->size; i++) {
for (hashmap_node_t *cur = map->buckets[i], *next; cur; cur = next) {
next = cur->next;
+ printf("cur: %x, cur->key: %x, cur->val: %x\n", cur, cur->key, cur->val);
free(cur->key);
free(cur->val);
free(cur);
cur = next;
}
}
+ printf("map: %x, map->buckets: %x\n", map, map->buckets);
free(map->buckets);
free(map);
} After adding
Notice that Therefore, the likely cause is that shecc handles pointer assignments incorrectly. |
void hashmap_free(hashmap_t *map)
{
for (int i = 0; i < map->size; i++) {
for (hashmap_node_t *cur = map->buckets[i], *next; cur; cur = next) {
next = cur->next;
+ printf("cur: %x, cur->key: %x, cur->val: %x\n", cur, cur->key, cur->val);
free(cur->key);
free(cur->val);
free(cur);
cur = next;
}
}
+ printf("map: %x, map->buckets: %x, map->buckets[0]\n", map, map->buckets, map->buckets[0]);
free(map->buckets);
free(map);
}
After further observation, I notice that Consider the issue #165, I think that the real bug is that using arrow operator and subscripting operator (-> and [ ]) simultaneously may obtain an incorrect object. |
Using the code below to initialize buckets for hashmap and frees hashmap:
Compile with gcc will exit normally, while compile with shecc will cause double free, which is abnormal.
The text was updated successfully, but these errors were encountered: