Merge pull request #4835 from dibdot/adblock
[feed/packages.git] / utils / bash / patches / 127-upstream-bash43-027.patch
1 BASH PATCH REPORT
2 =================
3
4 Bash-Release: 4.3
5 Patch-ID: bash43-027
6
7 Bug-Reported-by: Florian Weimer <fweimer@redhat.com>
8 Bug-Reference-ID:
9 Bug-Reference-URL:
10
11 Bug-Description:
12
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
16 function.
17
18 Patch (apply with `patch -p0'):
19
20 --- a/variables.c
21 +++ b/variables.c
22 @@ -83,6 +83,11 @@
23
24 #define ifsname(s) ((s)[0] == 'I' && (s)[1] == 'F' && (s)[2] == 'S' && (s)[3] == '\0')
25
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) */
30 +
31 extern char **environ;
32
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 *));
37
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
44
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))
52 {
53 + size_t namelen;
54 + char *tname; /* desired imported function name */
55 +
56 + namelen = char_index - BASHFUNC_PREFLEN - BASHFUNC_SUFFLEN;
57 +
58 + tname = name + BASHFUNC_PREFLEN; /* start of func name */
59 + tname[namelen] = '\0'; /* now tname == func name */
60 +
61 string_length = strlen (string);
62 - temp_string = (char *)xmalloc (3 + string_length + char_index);
63 + temp_string = (char *)xmalloc (namelen + string_length + 2);
64
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);
71
72 /* Don't import function names that are invalid identifiers from the
73 environment, though we still allow them to be defined as shell
74 variables. */
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);
79
80 - if (temp_var = find_function (name))
81 + if (temp_var = find_function (tname))
82 {
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;
87 }
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);
91 }
92 +
93 + /* Restore original suffix */
94 + tname[namelen] = BASHFUNC_SUFFIX[0];
95 }
96 #if defined (ARRAY_VARS)
97 # if ARRAY_EXPORT
98 @@ -2954,7 +2973,7 @@ assign_in_env (word, flags)
99 var->context = variable_context; /* XXX */
100
101 INVALIDATE_EXPORTSTR (var);
102 - var->exportstr = mk_env_string (name, value);
103 + var->exportstr = mk_env_string (name, value, 0);
104
105 array_needs_making = 1;
106
107 @@ -3852,21 +3871,42 @@ merge_temporary_env ()
108 /* **************************************************************** */
109
110 static inline char *
111 -mk_env_string (name, value)
112 +mk_env_string (name, value, isfunc)
113 const char *name, *value;
114 + int isfunc;
115 {
116 - int name_len, value_len;
117 - char *p;
118 + size_t name_len, value_len;
119 + char *p, *q;
120
121 name_len = strlen (name);
122 value_len = STRLEN (value);
123 - p = (char *)xmalloc (2 + name_len + value_len);
124 - strcpy (p, name);
125 - p[name_len] = '=';
126 +
127 + /* If we are exporting a shell function, construct the encoded function
128 + name. */
129 + if (isfunc && value)
130 + {
131 + p = (char *)xmalloc (BASHFUNC_PREFLEN + name_len + BASHFUNC_SUFFLEN + value_len + 2);
132 + q = p;
133 + memcpy (q, BASHFUNC_PREFIX, BASHFUNC_PREFLEN);
134 + q += BASHFUNC_PREFLEN;
135 + memcpy (q, name, name_len);
136 + q += name_len;
137 + memcpy (q, BASHFUNC_SUFFIX, BASHFUNC_SUFFLEN);
138 + q += BASHFUNC_SUFFLEN;
139 + }
140 + else
141 + {
142 + p = (char *)xmalloc (2 + name_len + value_len);
143 + memcpy (p, name, name_len);
144 + q = p + name_len;
145 + }
146 +
147 + q[0] = '=';
148 if (value && *value)
149 - strcpy (p + name_len + 1, value);
150 + memcpy (q + 1, value, value_len + 1);
151 else
152 - p[name_len + 1] = '\0';
153 + q[1] = '\0';
154 +
155 return (p);
156 }
157
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));
164
165 if (USE_EXPORTSTR == 0)
166 SAVE_EXPORTSTR (var, list[list_index]);
167 --- a/patchlevel.h
168 +++ b/patchlevel.h
169 @@ -25,6 +25,6 @@
170 regexp `^#define[ ]*PATCHLEVEL', since that's what support/mkversion.sh
171 looks for to find the patch level (for the sccs version string). */
172
173 -#define PATCHLEVEL 26
174 +#define PATCHLEVEL 27
175
176 #endif /* _PATCHLEVEL_H_ */