[tor-commits] [tor/master] Make _with_err return routerinfo, like old function does

nickm at torproject.org nickm at torproject.org
Mon May 14 18:14:26 UTC 2018


commit 36f7d0a940bbe8695d5343fccc1a0560a529883a
Author: rl1987 <rl1987 at sdf.lonestar.org>
Date:   Thu May 10 16:13:16 2018 +0300

    Make _with_err return routerinfo, like old function does
---
 src/or/policies.c      | 23 +++++++++++------------
 src/or/router.c        | 43 ++++++++++++++++++++++++++++---------------
 src/or/router.h        |  2 +-
 src/test/test_policy.c | 18 +++++++++++-------
 4 files changed, 51 insertions(+), 35 deletions(-)

diff --git a/src/or/policies.c b/src/or/policies.c
index 15176a97e..4d47985ec 100644
--- a/src/or/policies.c
+++ b/src/or/policies.c
@@ -2999,9 +2999,8 @@ getinfo_helper_policies(control_connection_t *conn,
     smartlist_free(private_policy_strings);
   } else if (!strcmp(question, "exit-policy/reject-private/relay")) {
     const or_options_t *options = get_options();
-    const routerinfo_t *me = NULL;
-
-    int err = router_get_my_routerinfo_with_err((routerinfo_t **)&me);
+    int err = 0;
+    const routerinfo_t *me = router_get_my_routerinfo_with_err(&err);
 
     if (!me) {
       *errmsg = routerinfo_errno_to_string(err);
@@ -3043,8 +3042,13 @@ getinfo_helper_policies(control_connection_t *conn,
     int include_ipv4 = 0;
     int include_ipv6 = 0;
 
-    const routerinfo_t *me = NULL;
-    int err = router_get_my_routerinfo_with_err((routerinfo_t **)&me);
+    int err = 0;
+    const routerinfo_t *me = router_get_my_routerinfo_with_err(&err);
+
+    if (!me) {
+      *errmsg = routerinfo_errno_to_string(err);
+      return routerinfo_err_is_transient(err) ? -1 : 0;
+    }
 
     if (!strcmp(question, "exit-policy/ipv4")) {
       include_ipv4 = 1;
@@ -3056,13 +3060,8 @@ getinfo_helper_policies(control_connection_t *conn,
       return 0; /* No such key. */
     }
 
-    if (!me) {
-      *errmsg = routerinfo_errno_to_string(err);
-      return routerinfo_err_is_transient(err) ? -1 : 0;
-    } else {
-      *answer = router_dump_exit_policy_to_string(me,include_ipv4,
-                                                  include_ipv6);
-    }
+    *answer = router_dump_exit_policy_to_string(me,include_ipv4,
+                                                include_ipv6);
   }
 
   return 0;
diff --git a/src/or/router.c b/src/or/router.c
index 612e23b3a..7bf40d69d 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -2073,28 +2073,41 @@ router_get_my_routerinfo,(void))
   return desc_routerinfo;
 }
 
-/** Set <b>ri</b> to routerinfo of this OR. Rebuild it from
- * scratch if needed. Return 0 on success or an appropriate
- * TOR_ROUTERINFO_ERROR_* value on failure.
+/** Return routerinfo of this OR. Rebuild it from
+ * scratch if needed. Set <b>*err</b> to 0 on success or to
+ * appropriate TOR_ROUTERINFO_ERROR_* value on failure.
  */
-MOCK_IMPL(int,
-router_get_my_routerinfo_with_err,(routerinfo_t **ri))
+MOCK_IMPL(const routerinfo_t *,
+router_get_my_routerinfo_with_err,(int *err))
 {
-  if (!server_mode(get_options()))
-    return TOR_ROUTERINFO_ERROR_NOT_A_SERVER;
+  if (!server_mode(get_options())) {
+    if (err)
+      *err = TOR_ROUTERINFO_ERROR_NOT_A_SERVER;
+
+    return NULL;
+  }
 
   if (!desc_clean_since) {
-    int err = router_rebuild_descriptor(0);
-    if (err < 0)
-      return err;
+    int rebuild_err = router_rebuild_descriptor(0);
+    if (rebuild_err < 0) {
+      if (err)
+        *err = rebuild_err;
+
+      return NULL;
+    }
   }
 
-  if (!desc_routerinfo)
-    return TOR_ROUTERINFO_ERROR_NOT_SO_FAST;
+  if (!desc_routerinfo) {
+    if (err)
+      *err = TOR_ROUTERINFO_ERROR_NOT_SO_FAST;
 
-  if (ri)
-    *ri = desc_routerinfo;
-  return 0;
+    return NULL;
+  }
+
+  if (err)
+    *err = 0;
+
+  return desc_routerinfo;
 }
 
 /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh
diff --git a/src/or/router.h b/src/or/router.h
index bf0267b77..561802c2c 100644
--- a/src/or/router.h
+++ b/src/or/router.h
@@ -92,7 +92,7 @@ void router_new_address_suggestion(const char *suggestion,
 int router_compare_to_my_exit_policy(const tor_addr_t *addr, uint16_t port);
 MOCK_DECL(int, router_my_exit_policy_is_reject_star,(void));
 MOCK_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
-MOCK_DECL(int, router_get_my_routerinfo_with_err,(routerinfo_t **ri));
+MOCK_DECL(const routerinfo_t *, router_get_my_routerinfo_with_err,(int *err));
 extrainfo_t *router_get_my_extrainfo(void);
 const char *router_get_my_descriptor(void);
 const char *router_get_descriptor_gen_reason(void);
diff --git a/src/test/test_policy.c b/src/test/test_policy.c
index aeddc1417..6ae57be8c 100644
--- a/src/test/test_policy.c
+++ b/src/test/test_policy.c
@@ -1498,16 +1498,20 @@ test_dump_exit_policy_to_string(void *arg)
 static routerinfo_t *mock_desc_routerinfo = NULL;
 static int routerinfo_err;
 
-static int
-mock_router_get_my_routerinfo_with_err(routerinfo_t **ri)
+static const routerinfo_t *
+mock_router_get_my_routerinfo_with_err(int *err)
 {
-  if (routerinfo_err)
-    return routerinfo_err;
+  if (routerinfo_err) {
+    if (err)
+      *err = routerinfo_err;
 
-  if (ri)
-    *ri = mock_desc_routerinfo;
+    return NULL;
+  }
 
-  return 0;
+  if (err)
+    *err = 0;
+
+  return mock_desc_routerinfo;
 }
 
 #define DEFAULT_POLICY_STRING "reject *:*"





More information about the tor-commits mailing list