Avoid using a dangling pointer when removing the last item
[openssl.git] / crypto / err / err.c
index e1091e3cf73f703d1c195b96021ca026a707d25e..9f81768ecc37a96671a31ffd9423a7527c2bbcdd 100644 (file)
 #include <stdio.h>
 #include <stdarg.h>
 #include <string.h>
-#include "cryptlib.h"
+#include "internal/cryptlib.h"
 #include <openssl/lhash.h>
 #include <openssl/crypto.h>
 #include <openssl/buffer.h>
@@ -157,6 +157,7 @@ static ERR_STRING_DATA ERR_str_libraries[] = {
     {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
     {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
     {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
+    {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"},
     {0, NULL},
 };
 
@@ -275,11 +276,9 @@ static LHASH_OF(ERR_STRING_DATA) *get_hash(int create, int lockit)
     if (lockit)
         CRYPTO_w_lock(CRYPTO_LOCK_ERR);
     if (!int_error_hash && create) {
-        CRYPTO_push_info("get_hash (err.c)");
         int_error_hash = lh_ERR_STRING_DATA_new();
-        CRYPTO_pop_info();
     }
-    if (int_error_hash)
+    if (int_error_hash != NULL)
         ret = int_error_hash;
     if (lockit)
         CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
@@ -322,11 +321,9 @@ static LHASH_OF(ERR_STATE) *int_thread_get(int create, int lockit)
     if (lockit)
         CRYPTO_w_lock(CRYPTO_LOCK_ERR);
     if (!int_thread_hash && create) {
-        CRYPTO_push_info("int_thread_get (err.c)");
         int_thread_hash = lh_ERR_STATE_new();
-        CRYPTO_pop_info();
     }
-    if (int_thread_hash) {
+    if (int_thread_hash != NULL) {
         int_thread_hash_references++;
         ret = int_thread_hash;
     }
@@ -402,8 +399,10 @@ static void int_thread_del_item(const ERR_STATE *d)
         if (int_thread_hash_references == 1
             && int_thread_hash
             && lh_ERR_STATE_num_items(int_thread_hash) == 0) {
+            int_thread_hash_references = 0;
             lh_ERR_STATE_free(int_thread_hash);
             int_thread_hash = NULL;
+            hash = NULL;
         }
     }
     CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
@@ -455,8 +454,8 @@ static void build_SYS_str_reasons(void)
             char (*dest)[LEN_SYS_STR_REASON] = &(strerror_tab[i - 1]);
             char *src = strerror(i);
             if (src != NULL) {
-                strncpy(*dest, src, sizeof *dest);
-                (*dest)[sizeof *dest - 1] = '\0';
+                strncpy(*dest, src, sizeof(*dest));
+                (*dest)[sizeof(*dest) - 1] = '\0';
                 str->string = *dest;
             }
         }
@@ -882,7 +881,7 @@ ERR_STATE *ERR_get_state(void)
 
     /* ret == the error state, if NULL, make a new one */
     if (ret == NULL) {
-        ret = OPENSSL_malloc(sizeof(ERR_STATE));
+        ret = OPENSSL_malloc(sizeof(*ret));
         if (ret == NULL)
             return (&fallback);
         CRYPTO_THREADID_cpy(&ret->tid, &tid);
@@ -967,7 +966,7 @@ void ERR_add_error_vdata(int num, va_list args)
                 }
                 str = p;
             }
-            BUF_strlcat(str, a, (size_t)s + 1);
+            OPENSSL_strlcat(str, a, (size_t)s + 1);
         }
     }
     ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING);