From 34708e2ef048f3788f3f2d5018735b27b156d244 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 27 Aug 2019 14:43:54 +0200 Subject: lib: accept %NULL arguments for nl_addr_cmp() Just be more forgiving. Also, this avoids a coverity warning: Error: FORWARD_NULL (CWE-476): [#def1] libnl-3.4.0/lib/route/addr.c:502: var_compare_op: Comparing "a->a_peer" to null implies that "a->a_peer" might be null. libnl-3.4.0/lib/route/addr.c:513: var_deref_model: Passing null pointer "a->a_peer" to "nl_addr_cmp", which dereferences it. libnl-3.4.0/lib/addr.c:587:8: deref_parm: Directly dereferencing parameter "a". # 585| int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b) # 586| { # 587|-> int d = a->a_family - b->a_family; # 588| # 589| if (d == 0) { https://bugzilla.redhat.com/show_bug.cgi?id=1606988 --- lib/addr.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/addr.c b/lib/addr.c index f24e827..06f3138 100644 --- a/lib/addr.c +++ b/lib/addr.c @@ -585,8 +585,16 @@ int nl_addr_shared(const struct nl_addr *addr) */ int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b) { - int d = a->a_family - b->a_family; + int d; + + if (a == b) + return 0; + if (!a) + return -1; + if (!b) + return 1; + d = a->a_family - b->a_family; if (d == 0) { d = a->a_len - b->a_len; -- cgit v0.12