summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2015-11-27 11:54:52 -0500
committerNick Mathewson <nickm@torproject.org>2015-11-27 11:54:52 -0500
commit85a48d5e471c62003d58e615bac8e1bd8c09631a (patch)
tree3caee310b657f2e9cbb4f01398e7bc0a87013216 /src
parenteedef419445098dbce1b25e782bbe023bc659902 (diff)
parent8ce70fcbf35630889e6acc64388476ea0c859a1e (diff)
downloadtor-85a48d5e471c62003d58e615bac8e1bd8c09631a.tar.gz
tor-85a48d5e471c62003d58e615bac8e1bd8c09631a.zip
Merge branch 'fix-policies-memory-v2-squashed'
Diffstat (limited to 'src')
-rw-r--r--src/or/policies.c71
-rw-r--r--src/test/test_policy.c2
2 files changed, 42 insertions, 31 deletions
diff --git a/src/or/policies.c b/src/or/policies.c
index d531b3ac78..126ba465df 100644
--- a/src/or/policies.c
+++ b/src/or/policies.c
@@ -1270,41 +1270,53 @@ policies_parse_exit_policy(config_line_t *cfg, smartlist_t **dest,
add_default);
}
-/** Helper function that adds addr to a smartlist as long as it is non-NULL
- * and not tor_addr_is_null(). */
+/** Helper function that adds a copy of addr to a smartlist as long as it is
+ * non-NULL and not tor_addr_is_null().
+ *
+ * The caller is responsible for freeing all the tor_addr_t* in the smartlist.
+ */
static void
-policies_add_addr_to_smartlist(smartlist_t *addr_list, const tor_addr_t *addr)
+policies_copy_addr_to_smartlist(smartlist_t *addr_list, const tor_addr_t *addr)
{
if (addr && !tor_addr_is_null(addr)) {
- smartlist_add(addr_list, (void *)addr);
+ tor_addr_t *addr_copy = tor_malloc(sizeof(tor_addr_t));
+ tor_addr_copy(addr_copy, addr);
+ smartlist_add(addr_list, addr_copy);
}
}
/** Helper function that adds ipv4h_addr to a smartlist as a tor_addr_t *,
- * by converting it to a tor_addr_t and passing it to
- * policies_add_addr_to_smartlist. */
+ * as long as it is not tor_addr_is_null(), by converting it to a tor_addr_t
+ * and passing it to policies_add_addr_to_smartlist.
+ *
+ * The caller is responsible for freeing all the tor_addr_t* in the smartlist.
+ */
static void
-policies_add_ipv4h_to_smartlist(smartlist_t *addr_list, uint32_t ipv4h_addr)
+policies_copy_ipv4h_to_smartlist(smartlist_t *addr_list, uint32_t ipv4h_addr)
{
if (ipv4h_addr) {
tor_addr_t ipv4_tor_addr;
tor_addr_from_ipv4h(&ipv4_tor_addr, ipv4h_addr);
- policies_add_addr_to_smartlist(addr_list, (void *)&ipv4_tor_addr);
+ policies_copy_addr_to_smartlist(addr_list, &ipv4_tor_addr);
}
}
-/** Helper function that adds or_options->OutboundBindAddressIPv[4|6]_ to a
- * smartlist as a tor_addr_t *, as long as or_options is non-NULL,
- * by passing them to policies_add_addr_to_smartlist. */
+/** Helper function that adds copies of
+ * or_options->OutboundBindAddressIPv[4|6]_ to a smartlist as tor_addr_t *, as
+ * long as or_options is non-NULL, and the addresses are not
+ * tor_addr_is_null(), by passing them to policies_add_addr_to_smartlist.
+ *
+ * The caller is responsible for freeing all the tor_addr_t* in the smartlist.
+ */
static void
-policies_add_outbound_addresses_to_smartlist(smartlist_t *addr_list,
- const or_options_t *or_options)
+policies_copy_outbound_addresses_to_smartlist(smartlist_t *addr_list,
+ const or_options_t *or_options)
{
if (or_options) {
- policies_add_addr_to_smartlist(addr_list,
- &or_options->OutboundBindAddressIPv4_);
- policies_add_addr_to_smartlist(addr_list,
- &or_options->OutboundBindAddressIPv6_);
+ policies_copy_addr_to_smartlist(addr_list,
+ &or_options->OutboundBindAddressIPv4_);
+ policies_copy_addr_to_smartlist(addr_list,
+ &or_options->OutboundBindAddressIPv6_);
}
}
@@ -1361,17 +1373,16 @@ policies_parse_exit_policy_from_options(const or_options_t *or_options,
parser_cfg |= EXIT_POLICY_ADD_DEFAULT;
}
- /* Add the configured addresses to the tor_addr_t* list */
- policies_add_ipv4h_to_smartlist(configured_addresses, local_address);
- policies_add_addr_to_smartlist(configured_addresses, ipv6_local_address);
- policies_add_outbound_addresses_to_smartlist(configured_addresses,
- or_options);
+ /* Copy the configured addresses into the tor_addr_t* list */
+ policies_copy_ipv4h_to_smartlist(configured_addresses, local_address);
+ policies_copy_addr_to_smartlist(configured_addresses, ipv6_local_address);
+ policies_copy_outbound_addresses_to_smartlist(configured_addresses,
+ or_options);
rv = policies_parse_exit_policy(or_options->ExitPolicy, result, parser_cfg,
configured_addresses);
- /* We don't need to free the pointers in this list, they are either constant
- * or locally scoped. */
+ SMARTLIST_FOREACH(configured_addresses, tor_addr_t *, a, tor_free(a));
smartlist_free(configured_addresses);
return rv;
@@ -2154,11 +2165,11 @@ getinfo_helper_policies(control_connection_t *conn,
smartlist_t *private_policy_list = smartlist_new();
smartlist_t *configured_addresses = smartlist_new();
- /* Add the configured addresses to the tor_addr_t* list */
- policies_add_ipv4h_to_smartlist(configured_addresses, me->addr);
- policies_add_addr_to_smartlist(configured_addresses, &me->ipv6_addr);
- policies_add_outbound_addresses_to_smartlist(configured_addresses,
- options);
+ /* Copy the configured addresses into the tor_addr_t* list */
+ policies_copy_ipv4h_to_smartlist(configured_addresses, me->addr);
+ policies_copy_addr_to_smartlist(configured_addresses, &me->ipv6_addr);
+ policies_copy_outbound_addresses_to_smartlist(configured_addresses,
+ options);
policies_parse_exit_policy_reject_private(
&private_policy_list,
@@ -2168,7 +2179,7 @@ getinfo_helper_policies(control_connection_t *conn,
*answer = policy_dump_to_string(private_policy_list, 1, 1);
addr_policy_list_free(private_policy_list);
- /* the addresses in configured_addresses are not ours to free */
+ SMARTLIST_FOREACH(configured_addresses, tor_addr_t *, a, tor_free(a));
smartlist_free(configured_addresses);
} else if (!strcmpstart(question, "exit-policy/")) {
const routerinfo_t *me = router_get_my_routerinfo();
diff --git a/src/test/test_policy.c b/src/test/test_policy.c
index 082f930551..b7d89a1445 100644
--- a/src/test/test_policy.c
+++ b/src/test/test_policy.c
@@ -1033,7 +1033,7 @@ test_policies_getinfo_helper_policies(void *arg)
tor_free(answer);
UNMOCK(get_options);
UNMOCK(router_get_my_routerinfo);
- smartlist_free(mock_my_routerinfo.exit_policy);
+ addr_policy_list_free(mock_my_routerinfo.exit_policy);
}
#undef DEFAULT_POLICY_STRING