Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill the xbt_memdup inline function since it's not exactly what we need, and
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Sun, 7 Aug 2005 17:26:10 +0000 (17:26 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Sun, 7 Aug 2005 17:26:10 +0000 (17:26 +0000)
changing its semantic without changing its name would be cumbersome.

Fix a long standing bug with by one offsets reported by valgrind around dicts.
YEAH. At least. I'm tracking this one since a *long* time.

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@1614 48e7efb5-ca39-0410-a469-dd3cf9ba447f

src/xbt/dict_elm.c

index 69ce028..6f9cc70 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:
  *
 /*
  * _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*);
 
       _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);
 
 
       _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;
 
   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) {
 
   /* there is no head, create it */
   if (!p_head) {