[tor-commits] [tor/master] Heap-allocate strings returned by get_current_process_environment_variables

nickm at torproject.org nickm at torproject.org
Fri Feb 17 16:46:49 UTC 2012


commit 33552c16ca3644e2dddd8ba6beee58aabd9d1977
Author: Robert Ransom <rransom.8774 at gmail.com>
Date:   Wed Feb 15 14:09:53 2012 -0800

    Heap-allocate strings returned by get_current_process_environment_variables
---
 src/common/util.c   |    6 +++---
 src/or/transports.c |   20 +++++++++++---------
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/src/common/util.c b/src/common/util.c
index 90ad8c0..509477a 100644
--- a/src/common/util.c
+++ b/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;
diff --git a/src/or/transports.c b/src/or/transports.c
index c10490e..726419e 100644
--- a/src/or/transports.c
+++ b/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;
 }
 





More information about the tor-commits mailing list