]> git.ipfire.org Git - thirdparty/openssl.git/blobdiff - test/exdatatest.c
Update copyright year
[thirdparty/openssl.git] / test / exdatatest.c
index 3ce6d33c1c89ea15724d3422e1a9a4e71d71e2ab..6ee916cb3d670fddb0ca6c3bcb98d0f609b14555 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -37,7 +37,7 @@ static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
 }
 
 static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
-          void *from_d, int idx, long argl, void *argp)
+          void **from_d, int idx, long argl, void *argp)
 {
     if (!TEST_int_eq(idx, saved_idx)
         || !TEST_long_eq(argl, saved_argl)
@@ -87,7 +87,7 @@ static void exnew2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
 }
 
 static int exdup2(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
-          void *from_d, int idx, long argl, void *argp)
+          void **from_d, int idx, long argl, void *argp)
 {
     MYOBJ_EX_DATA **update_ex_data = (MYOBJ_EX_DATA**)from_d;
     MYOBJ_EX_DATA *ex_data = NULL;
@@ -135,8 +135,10 @@ static MYOBJ *MYOBJ_new(void)
     static int count = 0;
     MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
 
-    obj->id = ++count;
-    obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
+    if (obj != NULL) {
+        obj->id = ++count;
+        obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
+    }
     return obj;
 }
 
@@ -199,31 +201,37 @@ static char *MYOBJ_gethello3(MYOBJ *obj)
 
 static void MYOBJ_free(MYOBJ *obj)
 {
-    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
-    OPENSSL_free(obj);
+    if (obj != NULL) {
+        CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
+        OPENSSL_free(obj);
+    }
 }
 
 static MYOBJ *MYOBJ_dup(MYOBJ *in)
 {
     MYOBJ *obj = MYOBJ_new();
 
-    obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
-                                 &in->ex_data);
+    if (obj != NULL)
+        obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
+                                     &in->ex_data);
     return obj;
 }
 
 static int test_exdata(void)
 {
-    MYOBJ *t1, *t2, *t3;
-    MYOBJ_EX_DATA *ex_data;
+    MYOBJ *t1 = NULL, *t2 = NULL, *t3 = NULL;
+    MYOBJ_EX_DATA *ex_data = NULL;
     const char *cp;
     char *p;
+    int res = 0;
 
     gbl_result = 1;
 
-    p = OPENSSL_strdup("hello world");
+    if (!TEST_ptr(p = OPENSSL_strdup("hello world")))
+        return 0;
     saved_argl = 21;
-    saved_argp = OPENSSL_malloc(1);
+    if (!TEST_ptr(saved_argp = OPENSSL_malloc(1)))
+        goto err;
     saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
                                         saved_argl, saved_argp,
                                         exnew, exdup, exfree);
@@ -233,9 +241,9 @@ static int test_exdata(void)
     t1 = MYOBJ_new();
     t2 = MYOBJ_new();
     if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
-        return 0;
+        goto err;
     if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))
-        return 0;
+        goto err;
 
     /*
      * saved_idx3 differs from other indexes by being created after the exdata
@@ -245,63 +253,63 @@ static int test_exdata(void)
                                          saved_argl, saved_argp,
                                          exnew2, exdup2, exfree2);
     if (!TEST_ptr_null(CRYPTO_get_ex_data(&t1->ex_data, saved_idx3)))
-        return 0;
+        goto err;
 
     MYOBJ_sethello(t1, p);
     cp = MYOBJ_gethello(t1);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
     MYOBJ_sethello2(t1, p);
     cp = MYOBJ_gethello2(t1);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
     MYOBJ_allochello3(t1, p);
     cp = MYOBJ_gethello3(t1);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
     cp = MYOBJ_gethello(t2);
     if (!TEST_ptr_null(cp))
-        return 0;
+        goto err;
 
     cp = MYOBJ_gethello2(t2);
     if (!TEST_ptr_null(cp))
-        return 0;
+        goto err;
 
     t3 = MYOBJ_dup(t1);
     if (!TEST_int_eq(t3->st, 1))
-        return 0;
+        goto err;
 
     ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2);
     if (!TEST_ptr(ex_data))
-        return 0;
+        goto err;
     if (!TEST_int_eq(ex_data->dup, 1))
-        return 0;
+        goto err;
 
     cp = MYOBJ_gethello(t3);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
     cp = MYOBJ_gethello2(t3);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
     cp = MYOBJ_gethello3(t3);
     if (!TEST_ptr_eq(cp, p))
-        return 0;
+        goto err;
 
+    if (gbl_result)
+        res = 1;
+ err:
     MYOBJ_free(t1);
     MYOBJ_free(t2);
     MYOBJ_free(t3);
     OPENSSL_free(saved_argp);
+    saved_argp = NULL;
     OPENSSL_free(p);
-
-    if (gbl_result)
-      return 1;
-    else
-      return 0;
+    return res;
 }
 
 int setup_tests(void)