Tidy up overflows in obj_cmp.
While this isn't really an issue, don't use the a - b comparator pattern since
it doesn't account for overflows. (They'll also break silently if that field
ever becomes unsigned as it should be.)
Change-Id: I613d19df6e4a785efd4cffd46e8b03dbc95b98e2
Reviewed-on: https://boringssl-review.googlesource.com/4890
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/obj/obj.c b/crypto/obj/obj.c
index 511aba3..bf16d17 100644
--- a/crypto/obj/obj.c
+++ b/crypto/obj/obj.c
@@ -167,18 +167,18 @@
return memcmp(a->data, b->data, a->length);
}
-/* nids_cmp is called to search the kNIDsInOIDOrder array. The |key| argument
- * is an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an
+/* obj_cmp is called to search the kNIDsInOIDOrder array. The |key| argument is
+ * an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an
* unsigned int in the array. */
static int obj_cmp(const void *key, const void *element) {
- int j;
- unsigned nid = *((unsigned*) element);
+ unsigned nid = *((const unsigned*) element);
const ASN1_OBJECT *a = key;
const ASN1_OBJECT *b = &kObjects[nid];
- j = a->length - b->length;
- if (j) {
- return j;
+ if (a->length < b->length) {
+ return -1;
+ } else if (a->length > b->length) {
+ return 1;
}
return memcmp(a->data, b->data, a->length);
}