UTHash multilevel hash table crashing on HASH_FIND_STR

1.6k Views Asked by At

I have two structures like so:

struct item2_map{
char stringkey[MAX_SIZE];
UT_hash_handle hh;
}

struct item1_map{
int key1;
struct item2_map *item2_map;
UT_hash_handle hh;
}

In my code I do something like this

struct item1_map *retrieved;
struct item2_map *found_value, *tmp;

HASH_FIND(hh, hash_head, key1, &someintvalue, sizeof(int), retrieved)
if(retrieved==NULL)
{
    HASH_ADD(hh, hash_head, key1, sizeof(key1), my_item1);
    my_item1->item2_map = NULL;
    HASH_ADD_STR(my_item1->item2_map, stringkey, my_item2);
} else 
{
    //THIS WORKS
    HASH_ITER(hh, retrieved->item2_map, found_value, tmp)
    { //do something }

    //THIS SEG FAULTS
    HASH_FIND_STR(retrieved->item2_map, &my_item2->stringkey, found_value)
}

This seems to give me a seg fault on HASH_FIND_STR(). Is there something I am doing wrong? In this example, assume that my_item1 and my_item2 came from somewhere else and are valid. I want to use stringkey as the key to find the values.

I put a breakpoint in the IF part of the conditional, so i Know that it is not found at first, then on the second find of that key, the else block is entered.

Interestingly enough, if I use HASH_ITER to iterate over the entries, it seems to "work" at least without crashing, though im not convinced all the values are the same.

1

There are 1 best solutions below

1
Troy D. Hanson On BEST ANSWER

It looks basically right. A few things to double check

  • check that my_item1->item2_map is initialized to NULL when my_item1 is created, before the HASH_ADD_STR
  • check that my_item2->stringkey is null-terminated and less than MAX_SIZE before HASH_FIND_STR
  • try running it under valgrind to see if there are any other clues

Let us know if you find anything.

Troy