Skip to content

kvetree_set fails when assigning "self" value #32

@rhaas80

Description

@rhaas80

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions