7 Bug-Reported-by: Florian Weimer <fweimer@redhat.com>
13 This patch changes the encoding bash uses for exported functions to avoid
14 clashes with shell variables and to avoid depending only on an environment
15 variable's contents to determine whether or not to interpret it as a shell
18 Patch (apply with `patch -p0'):
24 #define ifsname(s) ((s)[0] == 'I' && (s)[1] == 'F' && (s)[2] == 'S' && (s)[3] == '\0')
26 +#define BASHFUNC_PREFIX "BASH_FUNC_"
27 +#define BASHFUNC_PREFLEN 10 /* == strlen(BASHFUNC_PREFIX */
28 +#define BASHFUNC_SUFFIX "%%"
29 +#define BASHFUNC_SUFFLEN 2 /* == strlen(BASHFUNC_SUFFIX) */
31 extern char **environ;
33 /* Variables used here and defined in other files. */
34 @@ -279,7 +284,7 @@ static void push_temp_var __P((PTR_T));
35 static void propagate_temp_var __P((PTR_T));
36 static void dispose_temporary_env __P((sh_free_func_t *));
38 -static inline char *mk_env_string __P((const char *, const char *));
39 +static inline char *mk_env_string __P((const char *, const char *, int));
40 static char **make_env_array_from_var_list __P((SHELL_VAR **));
41 static char **make_var_export_array __P((VAR_CONTEXT *));
42 static char **make_func_export_array __P((void));
43 @@ -349,22 +354,33 @@ initialize_shell_variables (env, privmod
45 /* If exported function, define it now. Don't import functions from
46 the environment in privileged mode. */
47 - if (privmode == 0 && read_but_dont_execute == 0 && STREQN ("() {", string, 4))
48 + if (privmode == 0 && read_but_dont_execute == 0 &&
49 + STREQN (BASHFUNC_PREFIX, name, BASHFUNC_PREFLEN) &&
50 + STREQ (BASHFUNC_SUFFIX, name + char_index - BASHFUNC_SUFFLEN) &&
51 + STREQN ("() {", string, 4))
54 + char *tname; /* desired imported function name */
56 + namelen = char_index - BASHFUNC_PREFLEN - BASHFUNC_SUFFLEN;
58 + tname = name + BASHFUNC_PREFLEN; /* start of func name */
59 + tname[namelen] = '\0'; /* now tname == func name */
61 string_length = strlen (string);
62 - temp_string = (char *)xmalloc (3 + string_length + char_index);
63 + temp_string = (char *)xmalloc (namelen + string_length + 2);
65 - strcpy (temp_string, name);
66 - temp_string[char_index] = ' ';
67 - strcpy (temp_string + char_index + 1, string);
68 + memcpy (temp_string, tname, namelen);
69 + temp_string[namelen] = ' ';
70 + memcpy (temp_string + namelen + 1, string, string_length + 1);
72 /* Don't import function names that are invalid identifiers from the
73 environment, though we still allow them to be defined as shell
75 - if (legal_identifier (name))
76 - parse_and_execute (temp_string, name, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD);
77 + if (absolute_program (tname) == 0 && (posixly_correct == 0 || legal_identifier (tname)))
78 + parse_and_execute (temp_string, tname, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD);
80 - if (temp_var = find_function (name))
81 + if (temp_var = find_function (tname))
83 VSETATTR (temp_var, (att_exported|att_imported));
84 array_needs_making = 1;
85 @@ -377,8 +393,11 @@ initialize_shell_variables (env, privmod
86 array_needs_making = 1;
88 last_command_exit_value = 1;
89 - report_error (_("error importing function definition for `%s'"), name);
90 + report_error (_("error importing function definition for `%s'"), tname);
93 + /* Restore original suffix */
94 + tname[namelen] = BASHFUNC_SUFFIX[0];
96 #if defined (ARRAY_VARS)
98 @@ -2954,7 +2973,7 @@ assign_in_env (word, flags)
99 var->context = variable_context; /* XXX */
101 INVALIDATE_EXPORTSTR (var);
102 - var->exportstr = mk_env_string (name, value);
103 + var->exportstr = mk_env_string (name, value, 0);
105 array_needs_making = 1;
107 @@ -3852,21 +3871,42 @@ merge_temporary_env ()
108 /* **************************************************************** */
111 -mk_env_string (name, value)
112 +mk_env_string (name, value, isfunc)
113 const char *name, *value;
116 - int name_len, value_len;
118 + size_t name_len, value_len;
121 name_len = strlen (name);
122 value_len = STRLEN (value);
123 - p = (char *)xmalloc (2 + name_len + value_len);
127 + /* If we are exporting a shell function, construct the encoded function
129 + if (isfunc && value)
131 + p = (char *)xmalloc (BASHFUNC_PREFLEN + name_len + BASHFUNC_SUFFLEN + value_len + 2);
133 + memcpy (q, BASHFUNC_PREFIX, BASHFUNC_PREFLEN);
134 + q += BASHFUNC_PREFLEN;
135 + memcpy (q, name, name_len);
137 + memcpy (q, BASHFUNC_SUFFIX, BASHFUNC_SUFFLEN);
138 + q += BASHFUNC_SUFFLEN;
142 + p = (char *)xmalloc (2 + name_len + value_len);
143 + memcpy (p, name, name_len);
149 - strcpy (p + name_len + 1, value);
150 + memcpy (q + 1, value, value_len + 1);
152 - p[name_len + 1] = '\0';
158 @@ -3952,7 +3992,7 @@ make_env_array_from_var_list (vars)
159 /* Gee, I'd like to get away with not using savestring() if we're
160 using the cached exportstr... */
161 list[list_index] = USE_EXPORTSTR ? savestring (value)
162 - : mk_env_string (var->name, value);
163 + : mk_env_string (var->name, value, function_p (var));
165 if (USE_EXPORTSTR == 0)
166 SAVE_EXPORTSTR (var, list[list_index]);
170 regexp `^#define[ ]*PATCHLEVEL', since that's what support/mkversion.sh
171 looks for to find the patch level (for the sccs version string). */
173 -#define PATCHLEVEL 26
174 +#define PATCHLEVEL 27
176 #endif /* _PATCHLEVEL_H_ */