Pārlūkot izejas kodu

Heap-allocate strings returned by get_current_process_environment_variables

Robert Ransom 12 gadi atpakaļ
vecāks
revīzija
33552c16ca
2 mainītis faili ar 14 papildinājumiem un 12 dzēšanām
  1. 3 3
      src/common/util.c
  2. 11 9
      src/or/transports.c

+ 3 - 3
src/common/util.c

@@ -3832,8 +3832,8 @@ process_environment_make(struct smartlist_t *env_vars)
  * process can put strings not of that form in our environment;
  * callers should try to not get crashed by that.
  *
- * The returned strings are statically allocated, and must be treated
- * as read-only. */
+ * The returned strings are heap-allocated, and must be freed by the
+ * caller. */
 struct smartlist_t *
 get_current_process_environment_variables(void)
 {
@@ -3841,7 +3841,7 @@ get_current_process_environment_variables(void)
 
   char **environ_tmp; /* Not const char ** ? Really? */
   for (environ_tmp = environ; *environ_tmp; ++environ_tmp) {
-    smartlist_add(sl, (void *)(*environ_tmp));
+    smartlist_add(sl, tor_strdup(*environ_tmp));
   }
 
   return sl;

+ 11 - 9
src/or/transports.c

@@ -961,13 +961,14 @@ create_managed_proxy_environment(const managed_proxy_t *mp)
 {
   const or_options_t *options = get_options();
 
-  /* Environment variables to be added to or set in mp's environment.
-   * These are heap-allocated and are freed before this function
-   * returns. */
+  /* Environment variables to be added to or set in mp's environment. */
   smartlist_t *envs = smartlist_new();
+  /* XXXX The next time someone touches this code, shorten the name of
+   * set_environment_variable_in_smartlist, add a
+   * set_env_var_in_smartlist_asprintf function, and get rid of the
+   * silly extra envs smartlist. */
 
-  /* The final environment to be passed to mp.  Inherited variables are
-   * statically allocated; new ones are heap-allocated. */
+  /* The final environment to be passed to mp. */
   smartlist_t *merged_env_vars = get_current_process_environment_variables();
 
   process_environment_t *env;
@@ -1013,16 +1014,17 @@ create_managed_proxy_environment(const managed_proxy_t *mp)
   }
 
   SMARTLIST_FOREACH_BEGIN(envs, const char *, env_var) {
-    set_environment_variable_in_smartlist(merged_env_vars, env_var, NULL, 0);
+    set_environment_variable_in_smartlist(merged_env_vars, env_var,
+                                          _tor_free, 1);
   } SMARTLIST_FOREACH_END(env_var);
 
   env = process_environment_make(merged_env_vars);
 
-  smartlist_free(merged_env_vars);
-
-  SMARTLIST_FOREACH(envs, void *, x, tor_free(x));
   smartlist_free(envs);
 
+  SMARTLIST_FOREACH(merged_env_vars, void *, x, tor_free(x));
+  smartlist_free(merged_env_vars);
+
   return env;
 }