Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Actually obey the key_len argument in exception messages
[simgrid.git] / src / xbt / dict_elm.c
index 69ce028..267cf35 100644 (file)
@@ -67,18 +67,6 @@ _collapse_if_need(s_xbt_dictelm_t *p_head,
 
 /* ---- */
 
-static _XBT_INLINE
-void *
-xbt_memdup(const void * const ptr,
-           const size_t       length) {
-  void * new_ptr = NULL;
-
-  new_ptr = xbt_malloc(length);
-  memcpy(new_ptr, ptr, length);
-   
-  return new_ptr;
-}
-
 /*
  * _xbt_nibble_to_char:
  *
@@ -547,7 +535,11 @@ _xbt_dictelm_set_rec(s_xbt_dictelm_t     *p_head,
       _xbt_dictelm_alloc(key, key_len, offset, FALSE, data, free_f, &p_new);
       p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
 
-      anc_key = xbt_memdup(key, anc_key_len);
+      /* memdup the old key, taking care of the terminating NULL byte */
+      anc_key = xbt_malloc(anc_key_len+1);
+      memcpy(anc_key, key, anc_key_len);
+      anc_key[anc_key_len] = '\0';
+
 
       _xbt_dictelm_alloc(anc_key, anc_key_len, old_offset, TRUE, NULL, NULL, &p_anc);
 
@@ -593,7 +585,10 @@ xbt_dictelm_set_ext(s_xbt_dictelm_t **pp_head,
   s_xbt_dictelm_t  *p_head  = *pp_head;
   char         *key     =  NULL;
 
-  key = xbt_memdup(_key, key_len+1);
+  /* Make sure the copied key is NULL-terminated */
+  key = xbt_malloc(key_len+1);
+  memcpy(key, _key, key_len);
+  key[key_len] = '\0';
 
   /* there is no head, create it */
   if (!p_head) {
@@ -671,7 +666,7 @@ _xbt_dictelm_get_rec(s_xbt_dictelm_t *p_head,
     switch (match) {
 
     case 0: /* no child have a common prefix */
-      THROW1(not_found_error,0,"key '%s' not found",key);
+      THROW2(not_found_error,0,"key '%.*s' not found",key_len,key);
 
     case 1: /* A child have exactly this key => Got it */
       {
@@ -690,10 +685,10 @@ _xbt_dictelm_get_rec(s_xbt_dictelm_t *p_head,
       }
 
     case 3: /* The key is a prefix of the child => not found */
-      THROW1(not_found_error,0,"key %s not found",key);
+      THROW2(not_found_error,0,"key %.*s not found",key_len,key);
 
     case 4: /* A child share a common prefix with this key => not found */
-      THROW1(not_found_error,0,"key %s not found",key);
+      THROW2(not_found_error,0,"key %.*s not found",key_len,key);
 
     default:
       THROW_IMPOSSIBLE;
@@ -716,7 +711,7 @@ xbt_dictelm_get_ext(s_xbt_dictelm_t *p_head,
                    int              key_len) {
   /* there is no head, go to hell */
   if (!p_head)
-    THROW2(not_found_error,0,"Key '%*s' not found in dict",key_len,key);
+    THROW2(not_found_error,0,"Key '%.*s' not found in dict",key_len,key);
 
   return _xbt_dictelm_get_rec(p_head, key, key_len, 0);
 }
@@ -856,7 +851,7 @@ _xbt_dictelm_remove_rec(xbt_dictelm_t head,
     case 3: /* The key is a prefix of the child => not found */
     case 4: /* A child share a common prefix with this key => not found */
 
-      THROW2(not_found_error,0,"Unable to remove key '%*s': not found",key_len,key);
+      THROW2(not_found_error,0,"Unable to remove key '%.*s': not found",key_len,key);
 
     default:
       THROW_IMPOSSIBLE;