[Openvpn-devel,v2] Use more C99 initialization in add_route/add_route_ipv6().

Message ID 20210802152619.30754-1-gert@greenie.muc.de
State Accepted
Headers show
Series [Openvpn-devel,v2] Use more C99 initialization in add_route/add_route_ipv6(). | expand

Commit Message

Gert Doering Aug. 2, 2021, 5:26 a.m. UTC
This gets rid of a few #ifdef and also removes the need for
commit a11bea18b1c93 (argv is only initialized after the
early exit check on RT_DEFINED).

v2:
  use gc_new(), group gc and argv init in both add_route*() functions

Signed-off-by: Gert Doering <gert@greenie.muc.de>
---
 src/openvpn/route.c | 40 +++++++++++-----------------------------
 1 file changed, 11 insertions(+), 29 deletions(-)

Comments

Antonio Quartulli Aug. 3, 2021, 12:23 a.m. UTC | #1
Hi,

On 02/08/2021 17:26, Gert Doering wrote:
> This gets rid of a few #ifdef and also removes the need for
> commit a11bea18b1c93 (argv is only initialized after the
> early exit check on RT_DEFINED).
> 
> v2:
>   use gc_new(), group gc and argv init in both add_route*() functions
> 
> Signed-off-by: Gert Doering <gert@greenie.muc.de>

Thanks for addressing my comments!
Code looks good and compiles in my CI (linux and windows only)

No leaks detected during a basic test.

Acked-by: Antonio Quartulli <antonio@openvpn.net>
Gert Doering Aug. 5, 2021, 5:37 a.m. UTC | #2
Patch has been applied to the master branch.

commit 903e2cf5c1152834437c8d814ed17d0838552f72
Author: Gert Doering
Date:   Mon Aug 2 17:26:19 2021 +0200

     Use more C99 initialization in add_route/add_route_ipv6().

     Signed-off-by: Gert Doering <gert@greenie.muc.de>
     Acked-by: Antonio Quartulli <antonio@openvpn.net>
     Message-Id: <20210802152619.30754-1-gert@greenie.muc.de>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg22694.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/src/openvpn/route.c b/src/openvpn/route.c
index e429e8c0..2905e432 100644
--- a/src/openvpn/route.c
+++ b/src/openvpn/route.c
@@ -1570,32 +1570,23 @@  add_route(struct route_ipv4 *r,
           const struct env_set *es,
           openvpn_net_ctx_t *ctx)
 {
-    struct gc_arena gc;
-    struct argv argv = argv_new();
-#if !defined(TARGET_LINUX)
-    const char *network;
-#if !defined(TARGET_AIX)
-    const char *netmask;
-#endif
-    const char *gateway;
-#endif
     bool status = false;
     int is_local_route;
 
     if (!(r->flags & RT_DEFINED))
     {
-        argv_free(&argv);
         return;
     }
 
-    gc_init(&gc);
+    struct argv argv = argv_new();
+    struct gc_arena gc = gc_new();
 
 #if !defined(TARGET_LINUX)
-    network = print_in_addr_t(r->network, 0, &gc);
+    const char *network = print_in_addr_t(r->network, 0, &gc);
 #if !defined(TARGET_AIX)
-    netmask = print_in_addr_t(r->netmask, 0, &gc);
+    const char *netmask = print_in_addr_t(r->netmask, 0, &gc);
 #endif
-    gateway = print_in_addr_t(r->gateway, 0, &gc);
+    const char *gateway = print_in_addr_t(r->gateway, 0, &gc);
 #endif
 
     is_local_route = local_route(r->network, r->netmask, r->gateway, rgi);
@@ -1878,24 +1869,18 @@  add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt,
                unsigned int flags, const struct env_set *es,
                openvpn_net_ctx_t *ctx)
 {
-    struct gc_arena gc;
-    struct argv argv = argv_new();
-
-    const char *network;
-    const char *gateway;
     bool status = false;
     const char *device = tt->actual_name;
-#if defined(TARGET_LINUX)
-    int metric;
-#endif
     bool gateway_needed = false;
 
     if (!(r6->flags & RT_DEFINED) )
     {
-        argv_free(&argv);
         return;
     }
 
+    struct argv argv = argv_new();
+    struct gc_arena gc = gc_new();
+
 #ifndef _WIN32
     if (r6->iface != NULL)              /* vpn server special route */
     {
@@ -1907,12 +1892,9 @@  add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt,
     }
 #endif
 
-    gc_init(&gc);
-
     route_ipv6_clear_host_bits(r6);
-
-    network = print_in6_addr( r6->network, 0, &gc);
-    gateway = print_in6_addr( r6->gateway, 0, &gc);
+    const char *network = print_in6_addr( r6->network, 0, &gc);
+    const char *gateway = print_in6_addr( r6->gateway, 0, &gc);
 
 #if defined(TARGET_DARWIN)    \
     || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)    \
@@ -1963,7 +1945,7 @@  add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt,
     }
 
 #if defined(TARGET_LINUX)
-    metric = -1;
+    int metric = -1;
     if ((r6->flags & RT_METRIC_DEFINED) && (r6->metric > 0))
     {
         metric = r6->metric;