Bash-4.2 patch 11
[platform/upstream/bash.git] / assoc.c
diff --git a/assoc.c b/assoc.c
index 476facb..4561de4 100644 (file)
--- a/assoc.c
+++ b/assoc.c
@@ -77,6 +77,11 @@ assoc_insert (hash, key, value)
   b = hash_search (key, hash, HASH_CREATE);
   if (b == 0)
     return -1;
+  /* If we are overwriting an existing element's value, we're not going to
+     use the key.  Nothing in the array assignment code path frees the key
+     string, so we can free it here to avoid a memory leak. */
+  if (b->key != key)
+    free (key);
   FREE (b->data);
   b->data = value ? savestring (value) : (char *)0;
   return (0);
@@ -405,7 +410,14 @@ assoc_to_assign (hash, quoted)
   for (i = 0; i < hash->nbuckets; i++)
     for (tlist = hash_items (i, hash); tlist; tlist = tlist->next)
       {
+#if 1
+       if (sh_contains_shell_metas (tlist->key))
+         istr = sh_double_quote (tlist->key);
+       else
+         istr = tlist->key;    
+#else
        istr = tlist->key;
+#endif
        vstr = tlist->data ? sh_double_quote ((char *)tlist->data) : (char *)0;
 
        elen = STRLEN (istr) + 8 + STRLEN (vstr);
@@ -423,6 +435,10 @@ assoc_to_assign (hash, quoted)
          }
        ret[rlen++] = ' ';
 
+
+       if (istr != tlist->key)
+         FREE (istr);
+
        FREE (vstr);
     }