Skip to content

enhance fetchPut documentation #24268

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

enhance fetchPut documentation #24268

wants to merge 2 commits into from

Conversation

snirye
Copy link

@snirye snirye commented Jun 25, 2025

While working with the hash map's fetchPut function, I encountered a double free issue. Here's the relevant code:

const key = try fm.alloc.dupe(u8, uri);
const f = try fm.alloc.create(file);

f.* = file{
    .text = text,
    .tree = tree,
};

const prev_value = try fm.files_map.fetchPut(
    key,
    f,
);

if (prev_value) |e| {
    fm.destoryFile(e.value);
    fm.alloc.free(e.key);
}

Later, when destroying the map, I used:

while (it.next()) |f| {
    fm.destoryFile(f.value_ptr.*);
    fm.alloc.free(f.key_ptr.*);
}

This caused a double free error because it's not clearly documented that the key passed to fetchPut is not used when an entry with the same key already exists. The original key is retained, and the new one must be freed manually in that case. I missed this detail, leading to the key being freed twice: once in the overwrite logic, and again during map destruction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant