Skip to contentSkip to conversation

Use more C99 initialization in add_route/add_route_ipv6().

PATCH Superseded Gert Doering 2 participants 5 messages Jul 27, 2021 – Aug 5, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Use more C99 initialization in add_route/add_route_ipv6().

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).

---
 src/openvpn/route.c | 34 ++++++++++------------------------
 1 file changed, 10 insertions(+), 24 deletions(-)
src/openvpn/route.c +10 -24
index e429e8c0..bec5d6ff 100644
@@ -1570,32 +1570,24 @@ 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;
     }
+    struct argv argv = argv_new();
 
+    struct gc_arena gc;
     gc_init(&gc);
 
 #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);
@@ -1879,23 +1871,18 @@ add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt,
                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();
+
 #ifndef _WIN32
     if (r6->iface != NULL)              /* vpn server special route */
     {
@@ -1910,9 +1897,8 @@ add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt,
     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 +1949,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;
Signature · 1 line
2.26.3
#1 lore raw

Hi,

Quoted 26 lines · Gert Doering

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).

---
 src/openvpn/route.c | 34 ++++++++++------------------------
 1 file changed, 10 insertions(+), 24 deletions(-)
src/openvpn/route.c +0 -9
index e429e8c0..bec5d6ff 100644
@@ -1570,32 +1570,24 @@ 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

src/openvpn/route.c add_route(struct route_ipv4 *r, · line 1581

Quoted 10 lines
src/openvpn/route.c +1 -1
     bool status = false;
     int is_local_route;
 
     if (!(r->flags & RT_DEFINED))
     {
-        argv_free(&argv);
         return;
     }
+    struct argv argv = argv_new();
 

isn't it nicer to attach the two declarations and leave an empty line between them and the upper block? :-)

src/openvpn/route.c add_route(struct route_ipv4 *r, · line 1583

Quoted 2 lines
src/openvpn/route.c +1 -0
+    struct gc_arena gc;
     gc_init(&gc);

Since you are here, why not compressing these two lines in one:

   struct gc_arena gc = gc_new();

?

Quoted 14 lines
src/openvpn/route.c +3 -3
 #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);

src/openvpn/route.c add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt, · line 1873

Quoted 3 lines
src/openvpn/route.c +0 -0
@@ -1879,23 +1871,18 @@ add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt,
                openvpn_net_ctx_t *ctx)
 {
     struct gc_arena gc;

why not moving this gc declaration like you did above?

Quoted 36 lines
src/openvpn/route.c +4 -10
-    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();
+
 #ifndef _WIN32
     if (r6->iface != NULL)              /* vpn server special route */
     {
@@ -1910,9 +1897,8 @@ add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt,
     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)    \

src/openvpn/route.c add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt, · line 1955

Quoted 8 lines
src/openvpn/route.c +1 -1
@@ -1963,7 +1949,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;

The rest looks good!

I have compiled it on Linux and Windows, but I presume you've compiled it on other platforms as well.

Regards,

Signature · 1 line
Antonio Quartulli
#2 lore raw

PATCH v2 1/1 Use more C99 initialization in add_route/add_route_ipv6().

In reply to Gert Doering

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

---
 src/openvpn/route.c | 40 +++++++++++-----------------------------
 1 file changed, 11 insertions(+), 29 deletions(-)
src/openvpn/route.c +11 -29
index e429e8c0..2905e432 100644
@@ -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;
Signature · 1 line
2.26.3
Acked-by on v2 1/1
#3 lore raw

Hi,

Quoted 8 lines · Gert Doering

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

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

No leaks detected during a basic test.

Signature · 1 line
Antonio Quartulli
#4 lore raw
In reply to Gert Doering

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 <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg22694.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering