Skip to content
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

Open
ChAoSUnItY opened this issue Jan 19, 2025 · 0 comments
Open

Incorrect double free behavior #181

ChAoSUnItY opened this issue Jan 19, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@ChAoSUnItY
Copy link
Collaborator

Using the code below to initialize buckets for hashmap and frees hashmap:

#include <stdlib.h>

typedef struct hashmap_node {
    char *key;
    void *val;
    struct hashmap_node *next;
} hashmap_node_t;

typedef struct {
    int size;
    hashmap_node_t **buckets;
} hashmap_t;

hashmap_t *hashmap_create(int size)
{
    hashmap_t *map = malloc(sizeof(hashmap_t));
    map->size = size;
    map->buckets = malloc(size * sizeof(hashmap_node_t *));

    for (int i = 0; i < map->size; i++)
        map->buckets[i] = 0;

    return map;
}

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;
            free(cur->key);
            free(cur->val);
            free(cur);
            cur = next;
        }
    }

    free(map->buckets);
    free(map);
}

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]->next = NULL;
    hashmap_free(map);
    return 0;
}

Compile with gcc will exit normally, while compile with shecc will cause double free, which is abnormal.

@ChAoSUnItY ChAoSUnItY added the bug Something isn't working label Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant