Found while adjusting the fuzzer to test for the requirement to
add NUL bytes on the end of ASN1 Strings. If we end up with a 0
length object here we can end up in a crash with memcmp.
This makes this cmp function test comparison like our others
that are 0 length object safe.
Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
Reviewed-by: Norbert Pocs <norbertp@openssl.org>
MergeDate: Tue May 26 08:51:35 2026
(Merged from https://github.com/openssl/openssl/pull/31201)
{
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);
+ int cmp, len = (a->length <= b->length) ? a->length : b->length;
- return cmp ? cmp : a->length - b->length;
+ if (len > 0) {
+ cmp = memcmp(a->data, b->data, len);
+ if (cmp != 0)
+ return cmp;
+ }
+
+ return a->length - b->length;
}
static int IPAddressFamily_check_len(const IPAddressFamily *f)