diff options
author | Theo Buehler <tb@cvs.openbsd.org> | 2021-12-28 20:07:18 +0000 |
---|---|---|
committer | Theo Buehler <tb@cvs.openbsd.org> | 2021-12-28 20:07:18 +0000 |
commit | af246bf7cc22296396dfa3a6cf3a1edc86fa95b1 (patch) | |
tree | 381ef826e4641e185cf2be80e568e41b3ac0143d /lib/libcrypto/x509/x509_addr.c | |
parent | e6b03d0d6383da355d0e35506b4837d5c96dc06f (diff) |
Make IPAddressFamily_cmp() more pleasing on the eye
Define and use MINIMUM() instead of a ternary operator and separate
the code from the declarations. Also, we can spare a line to make the
return legible instead of squeezing it into another ternary operator.
addressFamily->data contains a two-bytes AFI and an optional one-byte
SAFI. This function currently also compares any trailing garbage that
may be present. Since comparison functions can't really error, this
needs to be checked bofore it is used. Such checks will be added in
subsequent commits.
ok jsing
Diffstat (limited to 'lib/libcrypto/x509/x509_addr.c')
-rw-r--r-- | lib/libcrypto/x509/x509_addr.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/lib/libcrypto/x509/x509_addr.c b/lib/libcrypto/x509/x509_addr.c index f0ef5b83116..5f31d7307f6 100644 --- a/lib/libcrypto/x509/x509_addr.c +++ b/lib/libcrypto/x509/x509_addr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: x509_addr.c,v 1.40 2021/12/28 19:59:33 tb Exp $ */ +/* $OpenBSD: x509_addr.c,v 1.41 2021/12/28 20:07:17 tb Exp $ */ /* * Contributed to the OpenSSL Project by the American Registry for * Internet Numbers ("ARIN"). @@ -1041,6 +1041,8 @@ X509v3_addr_get_range(IPAddressOrRange *aor, const unsigned afi, return afi_length; } +#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) + /* * Sort comparison function for a sequence of IPAddressFamily. * @@ -1057,9 +1059,14 @@ IPAddressFamily_cmp(const IPAddressFamily *const *a_, { const ASN1_OCTET_STRING *a = (*a_)->addressFamily; const ASN1_OCTET_STRING *b = (*b_)->addressFamily; - int len = ((a->length <= b->length) ? a->length : b->length); - int cmp = memcmp(a->data, b->data, len); - return cmp ? cmp : a->length - b->length; + int len, cmp; + + len = MINIMUM(a->length, b->length); + + if ((cmp = memcmp(a->data, b->data, len)) != 0) + return cmp; + + return a->length - b->length; } /* |