]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - assoc.c
saved background process status hash table loop fixes
[thirdparty/bash.git] / assoc.c
diff --git a/assoc.c b/assoc.c
index 4561de4247530f9f6d08d3ec7c6406a3b24b66a0..84a387c208a02059c723332011f3c8afce2d0afb 100644 (file)
--- a/assoc.c
+++ b/assoc.c
@@ -7,7 +7,7 @@
  * chet@ins.cwru.edu
  */
 
-/* Copyright (C) 2008,2009 Free Software Foundation, Inc.
+/* Copyright (C) 2008,2009,2011 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -65,7 +65,7 @@ assoc_flush (hash)
 {
   hash_flush (hash, 0);
 }
-     
+
 int
 assoc_insert (hash, key, value)
      HASH_TABLE *hash;
@@ -87,6 +87,29 @@ assoc_insert (hash, key, value)
   return (0);
 }
 
+/* Like assoc_insert, but returns b->data instead of freeing it */
+PTR_T
+assoc_replace (hash, key, value)
+     HASH_TABLE *hash;
+     char *key;
+     char *value;
+{
+  BUCKET_CONTENTS *b;
+  PTR_T t;
+
+  b = hash_search (key, hash, HASH_CREATE);
+  if (b == 0)
+    return (PTR_T)0;
+  /* 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);
+  t = b->data;
+  b->data = value ? savestring (value) : (char *)0;
+  return t;
+}
+
 void
 assoc_remove (hash, string)
      HASH_TABLE *hash;
@@ -254,7 +277,10 @@ int starsub, quoted;
   for (i = 1; l && i < start; i++)
     l = l->next;
   if (l == 0)
-    return ((char *)NULL);
+    {
+      dispose_words (save);
+      return ((char *)NULL);
+    }
   for (j = 0,h = t = l; l && j < nelem; j++)
     {
       t = l;
@@ -410,15 +436,19 @@ 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))
+       if (ansic_shouldquote (tlist->key))
+         istr = ansic_quote (tlist->key, 0, (int *)0);
+       else if (sh_contains_shell_metas (tlist->key))
          istr = sh_double_quote (tlist->key);
+       else if (ALL_ELEMENT_SUB (tlist->key[0]) && tlist->key[1] == '\0')
+         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;
+
+       vstr = tlist->data ? (ansic_shouldquote ((char *)tlist->data) ?
+                               ansic_quote ((char *)tlist->data, 0, (int *)0) :
+                               sh_double_quote ((char *)tlist->data))
+                          : (char *)0;
 
        elen = STRLEN (istr) + 8 + STRLEN (vstr);
        RESIZE_MALLOCED_BUFFER (ret, rlen, (elen+1), rsize, rsize);
@@ -510,7 +540,7 @@ assoc_to_string (h, sep, quoted)
     return (savestring (""));
 
   result = NULL;
-  list = NULL;
+  l = list = NULL;
   /* This might be better implemented directly, but it's simple to implement
      by converting to a word list first, possibly quoting the data, then
      using list_string */
@@ -528,6 +558,8 @@ assoc_to_string (h, sep, quoted)
   l = REVERSE_LIST(list, WORD_LIST *);
 
   result = l ? string_list_internal (l, sep) : savestring ("");
+  dispose_words (l);  
+
   return result;
 }