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

kvetree_set fails when assigning "self" value #32

Open
rhaas80 opened this issue Sep 22, 2019 · 1 comment
Open

kvetree_set fails when assigning "self" value #32

rhaas80 opened this issue Sep 22, 2019 · 1 comment

Comments

@rhaas80
Copy link

rhaas80 commented Sep 22, 2019

kvttree_set incorrectly frees elem->hash when a kvtree's value hash is being set to its current hash in:

/* this key already exists, delete its current hash and reset it */
if (elem->hash != NULL) {
   kvtree_delete(&elem->hash);
}
elem->hash = hash_value;

Ie this:

kvtree *hash = kvtree_new();
kvtree *value = kvtree_new();
kvtree_set(hash, "key", value);

// this fails
kvtree *current = kvtree_get(hash, "key");
kvtree_set(hash, "key", current);

A fix is (as in C++ with assignment of this in operator=) to check if the pointers hash_value and elem->hash in

if (elem->hash != NULL) {
are different and only free the old hash if so.

@adammoody
Copy link
Contributor

@rhaas80 , that's a good idea. I hadn't initially worried about supporting this use case, so there may be similar checks that need to be added to other parts of the code.

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

No branches or pull requests

2 participants