lib: fix prefix_cmp() return values

OMGWTFBBQ

Signed-off-by: David Lamparter <equinox@diac24.net>
This commit is contained in:
David Lamparter 2019-07-28 09:24:00 +02:00
parent 51e75ed228
commit 1315d74de9
2 changed files with 23 additions and 16 deletions

View File

@ -165,6 +165,13 @@ extern "C" {
_min_a < _min_b ? _min_a : _min_b; \ _min_a < _min_b ? _min_a : _min_b; \
}) })
#define numcmp(a, b) \
({ \
typeof(a) _cmp_a = (a); \
typeof(b) _cmp_b = (b); \
(_cmp_a < _cmp_b) ? -1 : ((_cmp_a > _cmp_b) ? 1 : 0); \
})
#ifndef offsetof #ifndef offsetof
#ifdef __compiler_offsetof #ifdef __compiler_offsetof
#define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE,MEMBER) #define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE,MEMBER)

View File

@ -712,10 +712,11 @@ int prefix_same(const struct prefix *p1, const struct prefix *p2)
} }
/* /*
* Return 0 if the network prefixes represented by the struct prefix * Return -1/0/1 comparing the prefixes in a way that gives a full/linear
* arguments are the same prefix, and 1 otherwise. Network prefixes * order.
* are considered the same if the prefix lengths are equal and the *
* network parts are the same. Host bits (which are considered masked * Network prefixes are considered the same if the prefix lengths are equal
* and the network parts are the same. Host bits (which are considered masked
* by the prefix length) are not significant. Thus, 10.0.0.1/8 and * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
* 10.0.0.2/8 are considered equivalent by this routine. Note that * 10.0.0.2/8 are considered equivalent by this routine. Note that
* this routine has the same return sense as strcmp (which is different * this routine has the same return sense as strcmp (which is different
@ -725,44 +726,43 @@ int prefix_cmp(const struct prefix *p1, const struct prefix *p2)
{ {
int offset; int offset;
int shift; int shift;
int i;
/* Set both prefix's head pointer. */ /* Set both prefix's head pointer. */
const uint8_t *pp1; const uint8_t *pp1;
const uint8_t *pp2; const uint8_t *pp2;
if (p1->family != p2->family) if (p1->family != p2->family)
return 1; return numcmp(p1->family, p2->family);
if (p1->family == AF_FLOWSPEC) { if (p1->family == AF_FLOWSPEC) {
pp1 = (const uint8_t *)p1->u.prefix_flowspec.ptr; pp1 = (const uint8_t *)p1->u.prefix_flowspec.ptr;
pp2 = (const uint8_t *)p2->u.prefix_flowspec.ptr; pp2 = (const uint8_t *)p2->u.prefix_flowspec.ptr;
if (p1->u.prefix_flowspec.prefixlen != if (p1->u.prefix_flowspec.prefixlen !=
p2->u.prefix_flowspec.prefixlen) p2->u.prefix_flowspec.prefixlen)
return 1; return numcmp(p1->u.prefix_flowspec.prefixlen,
p2->u.prefix_flowspec.prefixlen);
offset = p1->u.prefix_flowspec.prefixlen; offset = p1->u.prefix_flowspec.prefixlen;
while (offset--) while (offset--)
if (pp1[offset] != pp2[offset]) if (pp1[offset] != pp2[offset])
return 1; return numcmp(pp1[offset], pp2[offset]);
return 0; return 0;
} }
pp1 = p1->u.val; pp1 = p1->u.val;
pp2 = p2->u.val; pp2 = p2->u.val;
if (p1->prefixlen != p2->prefixlen) if (p1->prefixlen != p2->prefixlen)
return 1; return numcmp(p1->prefixlen, p2->prefixlen);
offset = p1->prefixlen / PNBBY; offset = p1->prefixlen / PNBBY;
shift = p1->prefixlen % PNBBY; shift = p1->prefixlen % PNBBY;
if (shift) i = memcmp(pp1, pp2, offset);
if (maskbit[shift] & (pp1[offset] ^ pp2[offset])) if (i)
return 1; return i;
while (offset--) return numcmp(pp1[offset] & maskbit[shift],
if (pp1[offset] != pp2[offset]) pp2[offset] & maskbit[shift]);
return 1;
return 0;
} }
/* /*