asn1_string_to_utf8 test: appease coverity
authortb <tb@openbsd.org>
Wed, 23 Nov 2022 08:51:05 +0000 (08:51 +0000)
committertb <tb@openbsd.org>
Wed, 23 Nov 2022 08:51:05 +0000 (08:51 +0000)
Check for ASN_STRING_to_UTF8() failure before checking it matches our
expectations. This should convey clearly that test->want_len is never
negative.

CID 377011

Diagnosed by jsing

regress/lib/libcrypto/asn1/asn1_string_to_utf8.c

index 2ead7b4..a87969d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: asn1_string_to_utf8.c,v 1.1 2022/05/16 20:53:20 tb Exp $ */
+/*     $OpenBSD: asn1_string_to_utf8.c,v 1.2 2022/11/23 08:51:05 tb Exp $ */
 /*
  * Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
  *
@@ -86,12 +86,18 @@ asn1_string_to_utf8_test(const struct asn1_string_to_utf8_test_case *test)
                goto err;
        }
 
-       if ((ret = ASN1_STRING_to_UTF8(&out, str)) != test->want_len) {
+       if ((ret = ASN1_STRING_to_UTF8(&out, str)) < 0) {
                warnx("ASN1_STRING_to_UTF8 failed: got %d, want %d", ret,
                    test->want_len);
                goto err;
        }
 
+       if (ret != test->want_len) {
+               warnx("ASN1_STRING_to_UTF8: got %d, want %d", ret,
+                   test->want_len);
+               goto err;
+       }
+
        if (memcmp(out, test->want, test->want_len) != 0) {
                warnx("memcmp failed");
                goto err;