Deduplicate the three copies of OBJ_cmp
While I'm here, align on the version that compares the lengths
explicitly, rather than subtract. The subtraction trick does actually
work, because the lengths can't be negative and we're two's complement
(so 0 - INT_MAX fits in int). But just comparing avoids needing to think
about it.
Change-Id: Ide6e3539a27e187bb1a405600c367bb8dd82197e
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/62545
Reviewed-by: Bob Beck <bbe@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
diff --git a/crypto/obj/obj.c b/crypto/obj/obj.c
index 67c73d4..9be3730 100644
--- a/crypto/obj/obj.c
+++ b/crypto/obj/obj.c
@@ -159,11 +159,10 @@
}
int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
- int ret;
-
- ret = a->length - b->length;
- if (ret) {
- return ret;
+ if (a->length < b->length) {
+ return -1;
+ } else if (a->length > b->length) {
+ return 1;
}
return OPENSSL_memcmp(a->data, b->data, a->length);
}
@@ -189,15 +188,7 @@
// unsigned int in the array.
static int obj_cmp(const void *key, const void *element) {
uint16_t nid = *((const uint16_t *)element);
- const ASN1_OBJECT *a = key;
- const ASN1_OBJECT *b = &kObjects[nid];
-
- if (a->length < b->length) {
- return -1;
- } else if (a->length > b->length) {
- return 1;
- }
- return OPENSSL_memcmp(a->data, b->data, a->length);
+ return OBJ_cmp(key, &kObjects[nid]);
}
int OBJ_obj2nid(const ASN1_OBJECT *obj) {
@@ -474,14 +465,6 @@
return OPENSSL_hash32(obj->data, obj->length);
}
-static int cmp_data(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
- int i = a->length - b->length;
- if (i) {
- return i;
- }
- return OPENSSL_memcmp(a->data, b->data, a->length);
-}
-
static uint32_t hash_short_name(const ASN1_OBJECT *obj) {
return OPENSSL_strhash(obj->sn);
}
@@ -509,7 +492,7 @@
global_added_by_nid = lh_ASN1_OBJECT_new(hash_nid, cmp_nid);
}
if (global_added_by_data == NULL) {
- global_added_by_data = lh_ASN1_OBJECT_new(hash_data, cmp_data);
+ global_added_by_data = lh_ASN1_OBJECT_new(hash_data, OBJ_cmp);
}
if (global_added_by_short_name == NULL) {
global_added_by_short_name =