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
This commit is contained in:
Thomas Haller 2019-08-27 14:43:54 +02:00
parent 194069516d
commit 34708e2ef0

View File

@ -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;