iplink_vrf: Save device index from response for return code

A recent commit changed rtnl_talk_* to return the response message in
allocated memory so callers need to free it. The change to name_is_vrf
did not save the device index which is pointing to a struct inside the
now allocated and freed memory resulting in garbage getting returned
in some cases.

Fix by using a stack variable to save the return value and only set
it to ifi->ifi_index after all checks are done and before the answer
buffer is freed.

Fixes: 86bf43c7c2 ("lib/libnetlink: update rtnl_talk to support malloc buff at run time")
Cc: Hangbin Liu <liuhangbin@gmail.com>
Cc: Phil Sutter <phil@nwl.cc>
Signed-off-by: David Ahern <dsahern@gmail.com>
Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
David Ahern 2018-06-01 08:50:16 -07:00 committed by Stephen Hemminger
parent 2884af6d37
commit 4c3493689e
1 changed files with 3 additions and 1 deletions

View File

@ -191,6 +191,7 @@ int name_is_vrf(const char *name)
struct rtattr *tb[IFLA_MAX+1]; struct rtattr *tb[IFLA_MAX+1];
struct rtattr *li[IFLA_INFO_MAX+1]; struct rtattr *li[IFLA_INFO_MAX+1];
struct ifinfomsg *ifi; struct ifinfomsg *ifi;
int ifindex = 0;
int len; int len;
addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1); addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name, strlen(name) + 1);
@ -218,7 +219,8 @@ int name_is_vrf(const char *name)
if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf")) if (strcmp(RTA_DATA(li[IFLA_INFO_KIND]), "vrf"))
goto out; goto out;
ifindex = ifi->ifi_index;
out: out:
free(answer); free(answer);
return ifi->ifi_index; return ifindex;
} }