462fb2
--- ../bash-4.2-orig/variables.c	2014-09-25 13:07:59.313209541 +0200
462fb2
+++ variables.c	2014-09-25 13:15:29.869420719 +0200
462fb2
@@ -268,7 +268,7 @@
462fb2
 static void propagate_temp_var __P((PTR_T));
462fb2
 static void dispose_temporary_env __P((sh_free_func_t *));     
462fb2
 
462fb2
-static inline char *mk_env_string __P((const char *, const char *));
462fb2
+static inline char *mk_env_string __P((const char *, const char *, int));
462fb2
 static char **make_env_array_from_var_list __P((SHELL_VAR **));
462fb2
 static char **make_var_export_array __P((VAR_CONTEXT *));
462fb2
 static char **make_func_export_array __P((void));
462fb2
@@ -301,6 +301,14 @@
462fb2
 #endif
462fb2
 }
462fb2
 
462fb2
+/* Prefix and suffix for environment variable names which contain
462fb2
+   shell functions. */
462fb2
+#define FUNCDEF_PREFIX "BASH_FUNC_"
462fb2
+#define FUNCDEF_PREFIX_LEN (strlen (FUNCDEF_PREFIX))
462fb2
+#define FUNCDEF_SUFFIX "()"
462fb2
+#define FUNCDEF_SUFFIX_LEN (strlen (FUNCDEF_SUFFIX))
462fb2
+
462fb2
+
462fb2
 /* Initialize the shell variables from the current environment.
462fb2
    If PRIVMODE is nonzero, don't import functions from ENV or
462fb2
    parse $SHELLOPTS. */
462fb2
@@ -338,28 +346,40 @@
462fb2
 
462fb2
       /* If exported function, define it now.  Don't import functions from
462fb2
 	 the environment in privileged mode. */
462fb2
-      if (privmode == 0 && read_but_dont_execute == 0 && STREQN ("() {", string, 4))
462fb2
-	{
462fb2
-	  string_length = strlen (string);
462fb2
-	  temp_string = (char *)xmalloc (3 + string_length + char_index);
462fb2
+      if (privmode == 0 && read_but_dont_execute == 0
462fb2
+	  && STREQN (FUNCDEF_PREFIX, name, FUNCDEF_PREFIX_LEN)
462fb2
+	  && STREQ (name + char_index - FUNCDEF_SUFFIX_LEN, FUNCDEF_SUFFIX)
462fb2
+	  && STREQN ("() {", string, 4))
462fb2
+	{
462fb2
+	  size_t name_length
462fb2
+	    = char_index - (FUNCDEF_PREFIX_LEN + FUNCDEF_SUFFIX_LEN);
462fb2
+	  char *temp_name = name + FUNCDEF_PREFIX_LEN;
462fb2
+	  /* Temporarily remove the suffix. */
462fb2
+	  temp_name[name_length] = '\0';
462fb2
 
462fb2
-	  strcpy (temp_string, name);
462fb2
-	  temp_string[char_index] = ' ';
462fb2
-	  strcpy (temp_string + char_index + 1, string);
462fb2
+	  string_length = strlen (string);
462fb2
+	  temp_string = (char *)xmalloc (name_length + 1 + string_length + 1);
462fb2
+	  memcpy (temp_string, temp_name, name_length);
462fb2
+	  temp_string[name_length] = ' ';
462fb2
+	  memcpy (temp_string + name_length + 1, string, string_length + 1);
462fb2
 
462fb2
 	  /* Don't import function names that are invalid identifiers from the
462fb2
 	     environment, though we still allow them to be defined as shell
462fb2
	     variables. */
462fb2
-	  if (legal_identifier (name))
462fb2
-	    parse_and_execute (temp_string, name, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD);
462fb2
+	  if (legal_identifier (temp_name))
462fb2
+	    parse_and_execute (temp_string, temp_name,
462fb2
+			       SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD);
462fb2
 
462fb2
-	  if (temp_var = find_function (name))
462fb2
+	  if (temp_var = find_function (temp_name))
462fb2
 	    {
462fb2
 	      VSETATTR (temp_var, (att_exported|att_imported));
462fb2
 	      array_needs_making = 1;
462fb2
 	    }
462fb2
 	  else
462fb2
 	    report_error (_("error importing function definition for `%s'"), name);
462fb2
+
462fb2
+	  /* Restore the original suffix. */
462fb2
+	  temp_name[name_length] = FUNCDEF_SUFFIX[0];
462fb2
 	}
462fb2
 #if defined (ARRAY_VARS)
462fb2
 #  if 0
462fb2
@@ -2537,7 +2557,7 @@
462fb2
   var->context = variable_context;	/* XXX */
462fb2
 
462fb2
   INVALIDATE_EXPORTSTR (var);
462fb2
-  var->exportstr = mk_env_string (name, value);
462fb2
+  var->exportstr = mk_env_string (name, value, 0);
462fb2
 
462fb2
   array_needs_making = 1;
462fb2
 
462fb2
@@ -3388,22 +3408,43 @@
462fb2
 /*								    */
462fb2
 /* **************************************************************** */
462fb2
 
462fb2
+/* Returns the string NAME=VALUE if !FUNCTIONP or if VALUE == NULL (in
462fb2
+   which case it is treated as empty).  Otherwise, decorate NAME with
462fb2
+   FUNCDEF_PREFIX and FUNCDEF_SUFFIX, and return a string of the form
462fb2
+   FUNCDEF_PREFIX NAME FUNCDEF_SUFFIX = VALUE (without spaces).  */
462fb2
 static inline char *
462fb2
-mk_env_string (name, value)
462fb2
+mk_env_string (name, value, functionp)
462fb2
      const char *name, *value;
462fb2
+     int functionp;
462fb2
 {
462fb2
-  int name_len, value_len;
462fb2
-  char	*p;
462fb2
+  size_t name_len, value_len;
462fb2
+  char *p, *q;
462fb2
 
462fb2
   name_len = strlen (name);
462fb2
   value_len = STRLEN (value);
462fb2
-  p = (char *)xmalloc (2 + name_len + value_len);
462fb2
-  strcpy (p, name);
462fb2
-  p[name_len] = '=';
462fb2
+  if (functionp && value != NULL)
462fb2
+    {
462fb2
+      p = (char *)xmalloc (FUNCDEF_PREFIX_LEN + name_len + FUNCDEF_SUFFIX_LEN
462fb2
+			   + 1 + value_len + 1);
462fb2
+      q = p;
462fb2
+      memcpy (q, FUNCDEF_PREFIX, FUNCDEF_PREFIX_LEN);
462fb2
+      q += FUNCDEF_PREFIX_LEN;
462fb2
+      memcpy (q, name, name_len);
462fb2
+      q += name_len;
462fb2
+      memcpy (q, FUNCDEF_SUFFIX, FUNCDEF_SUFFIX_LEN);
462fb2
+      q += FUNCDEF_SUFFIX_LEN;
462fb2
+    }
462fb2
+  else
462fb2
+    {
462fb2
+      p = (char *)xmalloc (name_len + 1 + value_len + 1);
462fb2
+      memcpy (p, name, name_len);
462fb2
+      q = p + name_len;
462fb2
+    }
462fb2
+  q[0] = '=';
462fb2
   if (value && *value)
462fb2
-    strcpy (p + name_len + 1, value);
462fb2
+    memcpy (q + 1, value, value_len + 1);
462fb2
   else
462fb2
-    p[name_len + 1] = '\0';
462fb2
+    q[1] = '\0';
462fb2
   return (p);
462fb2
 }
462fb2
 
462fb2
@@ -3489,7 +3530,7 @@
462fb2
 	  /* Gee, I'd like to get away with not using savestring() if we're
462fb2
 	     using the cached exportstr... */
462fb2
 	  list[list_index] = USE_EXPORTSTR ? savestring (value)
462fb2
-					   : mk_env_string (var->name, value);
462fb2
+	    : mk_env_string (var->name, value, function_p (var));
462fb2
 
462fb2
 	  if (USE_EXPORTSTR == 0)
462fb2
 	    SAVE_EXPORTSTR (var, list[list_index]);