Skip to content

Commit

Permalink
nl_l3: fix nh_stub::operator<() strict weak ordering
Browse files Browse the repository at this point in the history
According to [1], std::set requires a Compare function that has strict
weak ordering [2], but the current implementation violates the second
requirement:

* If comp(a, b) == true then comp(b, a) == false.

E.g. given the nh_stubs a = <169.254.0.1,62> and b = <172.16.111.1,4>:

* comp(a, b) is true, as 172.16.111.1 isn't < 169.254.0.1, but 4 < 62
* comp(b, a) is true, as 169.254.0.1 < 172.16.111.1.

This breaks e.g. lookups in sets, causing element not being deleted or
being found.

Fix this by comparing the ifindex only if the addresses are equal, not
only if the other one's is larger.

[1] https://en.cppreference.com/w/cpp/container/set
[2] https://en.cppreference.com/w/cpp/named_req/Compare

Fixes: 0fdba0d ("nl_l3: only route l3 neighs if we have a route for them")
Signed-off-by: Jonas Gorski <jonas.gorski@bisdn.de>
  • Loading branch information
KanjiMonster committed Apr 29, 2024
1 parent 1966d9a commit 8a24381
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/netlink/nl_l3_interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ struct nh_stub {
}

bool operator<(const nh_stub &other) const {
if (nl_addr_cmp(nh, other.nh) < 0)
int cmp = nl_addr_cmp(nh, other.nh);

if (cmp < 0)
return true;
if (cmp > 0)
return false;

return ifindex < other.ifindex;
}
Expand Down

0 comments on commit 8a24381

Please sign in to comment.