Skip to content

Commit fe179fb

Browse files
committed
nexthop: Use _nl_auto_rtnl_nexthop in rtnl_route_nh_clone()
For better and less error-prone failure handling. Signed-off-by: Christoph Paasch <[email protected]>
1 parent 59c0f45 commit fe179fb

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

lib/route/nexthop.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
#include <netlink/route/route.h>
1818

1919
#include "nexthop-encap.h"
20-
#include "nl-route.h"
20+
#include "nl-aux-route/nl-route.h"
2121
#include "nl-priv-dynamic-core/nl-core.h"
22+
#include "nl-route.h"
2223

2324
/** @cond SKIP */
2425
#define NH_ATTR_FLAGS 0x000001
@@ -51,7 +52,7 @@ struct rtnl_nexthop *rtnl_route_nh_alloc(void)
5152

5253
struct rtnl_nexthop *rtnl_route_nh_clone(struct rtnl_nexthop *src)
5354
{
54-
struct rtnl_nexthop *nh;
55+
_nl_auto_rtnl_nexthop struct rtnl_nexthop *nh = NULL;
5556

5657
nh = rtnl_route_nh_alloc();
5758
if (!nh)
@@ -66,32 +67,23 @@ struct rtnl_nexthop *rtnl_route_nh_clone(struct rtnl_nexthop *src)
6667

6768
if (src->rtnh_gateway) {
6869
nh->rtnh_gateway = nl_addr_clone(src->rtnh_gateway);
69-
if (!nh->rtnh_gateway) {
70-
free(nh);
70+
if (!nh->rtnh_gateway)
7171
return NULL;
72-
}
7372
}
7473

7574
if (src->rtnh_newdst) {
7675
nh->rtnh_newdst = nl_addr_clone(src->rtnh_newdst);
77-
if (!nh->rtnh_newdst) {
78-
nl_addr_put(nh->rtnh_gateway);
79-
free(nh);
76+
if (!nh->rtnh_newdst)
8077
return NULL;
81-
}
8278
}
8379

8480
if (src->rtnh_via) {
8581
nh->rtnh_via = nl_addr_clone(src->rtnh_via);
86-
if (!nh->rtnh_via) {
87-
nl_addr_put(nh->rtnh_gateway);
88-
nl_addr_put(nh->rtnh_newdst);
89-
free(nh);
82+
if (!nh->rtnh_via)
9083
return NULL;
91-
}
9284
}
9385

94-
return nh;
86+
return _nl_steal_pointer(&nh);
9587
}
9688

9789
void rtnl_route_nh_free(struct rtnl_nexthop *nh)

0 commit comments

Comments
 (0)