Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a 3 years old bug in the dictionary's function xbt_dict_get_or_null().
[simgrid.git] / src / xbt / dict.c
index 8d9fa7e..708cffb 100644 (file)
@@ -89,7 +89,7 @@ void xbt_dict_free(xbt_dict_t * dict)
  */
 XBT_INLINE unsigned int xbt_dict_size(xbt_dict_t dict)
 {
-  return dict->count;
+  return (dict ? (unsigned int) dict->count : (unsigned int) 0);
 }
 
 /**
@@ -411,7 +411,7 @@ XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
 
   current = dict->table[hash_code & dict->table_size];
   while (current != NULL &&
-         hash_code != current->hash_code && strcmp(key, current->key))
+         (hash_code != current->hash_code || strcmp(key, current->key)))
     current = current->next;
 
   if (current == NULL)
@@ -637,7 +637,13 @@ void xbt_dict_dump_output_string(void *s)
 {
   fputs(s, stdout);
 }
-
+/**
+ * \brief test if the dict is empty or not
+ */
+XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
+{
+       return (xbt_dict_size(dict) == 0);
+}
 
 /**
  * \brief Outputs the content of the structure (debugging purpose)
@@ -1161,6 +1167,7 @@ XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
 }
 
 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data) {
+       uintptr_t stored_data = 0;
   xbt_test_log2("Add %zu under %zu", data, key);
 
   xbt_dicti_set(head, key, data);
@@ -1168,7 +1175,7 @@ static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data) {
     xbt_dict_dump(head, (void (*)(void *)) &printf);
     fflush(stdout);
   }
-  uintptr_t stored_data = xbt_dicti_get(head, key);
+  stored_data = xbt_dicti_get(head, key);
   xbt_test_assert3(stored_data==data,
       "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",stored_data,data,key);
 }