[tor-commits] [tor/master] Remove current_consensus macro.

nickm at torproject.org nickm at torproject.org
Fri Oct 14 14:25:13 UTC 2016


commit 2196c7ad6496ae699667af65eb289950f8fe5dbb
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Sep 20 10:52:20 2016 -0400

    Remove current_consensus macro.
    
    It's a macro that calls down to a function whose behavior has been
    getting progresively more complicated.... but we named it as if it
    were a variable.  That's not so smart.  So, replace it with a
    function call to a function that was just doing "return
    current_consensus".
    
    Fixes bug 20176.
---
 src/or/networkstatus.c | 47 +++++++++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 26 deletions(-)

diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c
index 28f5b42..0a32012 100644
--- a/src/or/networkstatus.c
+++ b/src/or/networkstatus.c
@@ -42,14 +42,6 @@ static strmap_t *named_server_map = NULL;
  * as unnamed for some server in the consensus. */
 static strmap_t *unnamed_server_map = NULL;
 
-/** Most recently received and validated v3 consensus network status,
- * of whichever type we are using for our own circuits.  This will be the same
- * as one of current_ns_consensus or current_md_consensus.
- */
-#define current_consensus                                       \
-  (we_use_microdescriptors_for_circuits(get_options()) ?        \
-   current_md_consensus : current_ns_consensus)
-
 /** Most recently received and validated v3 "ns"-flavored consensus network
  * status. */
 static networkstatus_t *current_ns_consensus = NULL;
@@ -130,7 +122,7 @@ static void update_consensus_bootstrap_multiple_downloads(
 void
 networkstatus_reset_warnings(void)
 {
-  if (current_consensus) {
+  if (networkstatus_get_latest_consensus()) {
     SMARTLIST_FOREACH(nodelist_get_list(), node_t *, node,
                       node->name_lookup_warned = 0);
   }
@@ -206,7 +198,7 @@ router_reload_consensus_networkstatus(void)
     tor_free(filename);
   }
 
-  if (!current_consensus) {
+  if (!networkstatus_get_latest_consensus()) {
     if (!named_server_map)
       named_server_map = strmap_new();
     if (!unnamed_server_map)
@@ -647,7 +639,7 @@ router_get_mutable_consensus_status_by_descriptor_digest,(
                                                  const char *digest))
 {
   if (!consensus)
-    consensus = current_consensus;
+    consensus = networkstatus_get_latest_consensus();
   if (!consensus)
     return NULL;
   if (!consensus->desc_digest_map) {
@@ -728,9 +720,9 @@ router_get_dl_status_by_descriptor_digest,(const char *d))
 routerstatus_t *
 router_get_mutable_consensus_status_by_id(const char *digest)
 {
-  if (!current_consensus)
+  if (!networkstatus_get_latest_consensus())
     return NULL;
-  return smartlist_bsearch(current_consensus->routerstatus_list, digest,
+  return smartlist_bsearch(networkstatus_get_latest_consensus()->routerstatus_list, digest,
                            compare_digest_to_routerstatus_entry);
 }
 
@@ -1280,7 +1272,10 @@ networkstatus_get_dl_status_by_flavor_running,(consensus_flavor_t flavor))
 MOCK_IMPL(networkstatus_t *,
 networkstatus_get_latest_consensus,(void))
 {
-  return current_consensus;
+  if (we_use_microdescriptors_for_circuits(get_options()))
+    return current_md_consensus;
+  else
+    return current_ns_consensus;
 }
 
 /** Return the latest consensus we have whose flavor matches <b>f</b>, or NULL
@@ -1303,10 +1298,10 @@ networkstatus_get_latest_consensus_by_flavor,(consensus_flavor_t f))
 MOCK_IMPL(networkstatus_t *,
 networkstatus_get_live_consensus,(time_t now))
 {
-  if (current_consensus &&
-      current_consensus->valid_after <= now &&
-      now <= current_consensus->valid_until)
-    return current_consensus;
+  if (networkstatus_get_latest_consensus() &&
+      networkstatus_get_latest_consensus()->valid_after <= now &&
+      now <= networkstatus_get_latest_consensus()->valid_until)
+    return networkstatus_get_latest_consensus();
   else
     return NULL;
 }
@@ -1736,13 +1731,13 @@ networkstatus_set_current_consensus(const char *consensus,
   const int is_usable_flavor = flav == usable_consensus_flavor();
 
   if (is_usable_flavor) {
-    notify_control_networkstatus_changed(current_consensus, c);
+    notify_control_networkstatus_changed(networkstatus_get_latest_consensus(), c);
   }
   if (flav == FLAV_NS) {
     if (current_ns_consensus) {
       networkstatus_copy_old_consensus_info(c, current_ns_consensus);
       networkstatus_vote_free(current_ns_consensus);
-      /* Defensive programming : we should set current_consensus very soon,
+      /* Defensive programming : we should set networkstatus_get_latest_consensus() very soon,
        * but we're about to call some stuff in the meantime, and leaving this
        * dangling pointer around has proven to be trouble. */
       current_ns_consensus = NULL;
@@ -1804,7 +1799,7 @@ networkstatus_set_current_consensus(const char *consensus,
     connection_or_update_token_buckets(get_connection_array(), options);
 
     circuit_build_times_new_consensus_params(get_circuit_build_times_mutable(),
-        current_consensus);
+        networkstatus_get_latest_consensus());
   }
 
   /* Reset the failure count only if this consensus is actually valid. */
@@ -1954,14 +1949,14 @@ routers_update_all_from_networkstatus(time_t now, int dir_version)
 static void
 routerstatus_list_update_named_server_map(void)
 {
-  if (!current_consensus)
+  if (!networkstatus_get_latest_consensus())
     return;
 
   strmap_free(named_server_map, tor_free_);
   named_server_map = strmap_new();
   strmap_free(unnamed_server_map, NULL);
   unnamed_server_map = strmap_new();
-  SMARTLIST_FOREACH_BEGIN(current_consensus->routerstatus_list,
+  SMARTLIST_FOREACH_BEGIN(networkstatus_get_latest_consensus()->routerstatus_list,
                           const routerstatus_t *, rs) {
       if (rs->is_named) {
         strmap_set_lc(named_server_map, rs->nickname,
@@ -1982,7 +1977,7 @@ routers_update_status_from_consensus_networkstatus(smartlist_t *routers,
 {
   const or_options_t *options = get_options();
   int authdir = authdir_mode_v3(options);
-  networkstatus_t *ns = current_consensus;
+  networkstatus_t *ns = networkstatus_get_latest_consensus();
   if (!ns || !smartlist_len(ns->routerstatus_list))
     return;
 
@@ -2298,14 +2293,14 @@ getinfo_helper_networkstatus(control_connection_t *conn,
   const routerstatus_t *status;
   (void) conn;
 
-  if (!current_consensus) {
+  if (!networkstatus_get_latest_consensus()) {
     *answer = tor_strdup("");
     return 0;
   }
 
   if (!strcmp(question, "ns/all")) {
     smartlist_t *statuses = smartlist_new();
-    SMARTLIST_FOREACH(current_consensus->routerstatus_list,
+    SMARTLIST_FOREACH(networkstatus_get_latest_consensus()->routerstatus_list,
                       const routerstatus_t *, rs,
       {
         smartlist_add(statuses, networkstatus_getinfo_helper_single(rs));





More information about the tor-commits mailing list