"Andrew W. Gray" <agray@iconsinc.com> says /GD is no longer a valid
[openssl.git] / crypto / mem_dbg.c
index 61329b098d8d89729df314ae6c60af055b293cab..a6c70e431a9f67ed5781b384a5b9e281da48e456 100644 (file)
@@ -219,31 +219,37 @@ long CRYPTO_dbg_get_options(void)
        return options;
        }
 
-static int mem_cmp(MEM *a, MEM *b)
+/* static int mem_cmp(MEM *a, MEM *b) */
+static int mem_cmp(const void *a_void, const void *b_void)
        {
-       return((char *)a->addr - (char *)b->addr);
+       return((const char *)((MEM *)a_void)->addr
+               - (const char *)((MEM *)b_void)->addr);
        }
 
-static unsigned long mem_hash(MEM *a)
+/* static unsigned long mem_hash(MEM *a) */
+static unsigned long mem_hash(const void *a_void)
        {
        unsigned long ret;
 
-       ret=(unsigned long)a->addr;
+       ret=(unsigned long)((const MEM *)a_void)->addr;
 
        ret=ret*17851+(ret>>14)*7+(ret>>4)*251;
        return(ret);
        }
 
-static int app_info_cmp(APP_INFO *a, APP_INFO *b)
+/* static int app_info_cmp(APP_INFO *a, APP_INFO *b) */
+static int app_info_cmp(const void *a_void, const void *b_void)
        {
-       return(a->thread != b->thread);
+       return(((const APP_INFO *)a_void)->thread
+               != ((const APP_INFO *)b_void)->thread);
        }
 
-static unsigned long app_info_hash(APP_INFO *a)
+/* static unsigned long app_info_hash(APP_INFO *a) */
+static unsigned long app_info_hash(const void *a_void)
        {
        unsigned long ret;
 
-       ret=(unsigned long)a->thread;
+       ret=(unsigned long)((const APP_INFO *)a_void)->thread;
 
        ret=ret*17851+(ret>>14)*7+(ret>>4)*251;
        return(ret);
@@ -302,7 +308,7 @@ int CRYPTO_push_info_(const char *info, const char *file, int line)
                        }
                if (amih == NULL)
                        {
-                       if ((amih=lh_new(app_info_hash,app_info_cmp)) == NULL)
+                       if ((amih=lh_new(app_info_hash, app_info_cmp)) == NULL)
                                {
                                OPENSSL_free(ami);
                                ret=0;
@@ -394,7 +400,7 @@ void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line,
                                }
                        if (mh == NULL)
                                {
-                               if ((mh=lh_new(mem_hash,mem_cmp)) == NULL)
+                               if ((mh=lh_new(mem_hash, mem_cmp)) == NULL)
                                        {
                                        OPENSSL_free(addr);
                                        OPENSSL_free(m);
@@ -647,7 +653,8 @@ void CRYPTO_mem_leaks(BIO *b)
        ml.chunks=0;
        MemCheck_off(); /* obtains CRYPTO_LOCK_MALLOC2 */
        if (mh != NULL)
-               lh_doall_arg(mh,(void (*)())print_leak,(char *)&ml);
+               lh_doall_arg(mh, (LHASH_DOALL_ARG_FN_TYPE)print_leak,
+                               (char *)&ml);
        if (ml.chunks != 0)
                {
                sprintf(buf,"%ld bytes leaked in %d chunks\n",
@@ -671,7 +678,15 @@ void CRYPTO_mem_leaks(BIO *b)
                 * void_fn_to_char kludge in CRYPTO_mem_leaks_cb.
                 * Otherwise the code police will come and get us.)
                 */
+               int old_mh_mode;
+
                CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
+
+               /* avoid deadlock when lh_free() uses CRYPTO_dbg_free(),
+                * which uses CRYPTO_is_mem_check_on */
+               old_mh_mode = mh_mode;
+               mh_mode = CRYPTO_MEM_CHECK_OFF;
+
                if (mh != NULL)
                        {
                        lh_free(mh);
@@ -685,6 +700,8 @@ void CRYPTO_mem_leaks(BIO *b)
                                amih = NULL;
                                }
                        }
+
+               mh_mode = old_mh_mode;
                CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
                }
        MemCheck_on(); /* releases CRYPTO_LOCK_MALLOC2 */
@@ -725,6 +742,6 @@ void CRYPTO_mem_leaks_cb(void (*cb)(unsigned long, const char *, int, int, void
        {
        if (mh == NULL) return;
        CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);
-       lh_doall_arg(mh,(void (*)())cb_leak,(void *)&cb);
+       lh_doall_arg(mh, (LHASH_DOALL_ARG_FN_TYPE)cb_leak,(void *)&cb);
        CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
        }