Fix typo in SSL_pending docs
[openssl.git] / crypto / mem_dbg.c
index 2bdfbffba27e152e69b91c3dfc50dd497408893d..f2a1fd837ab3f0d892caa2de96fda0fded79ee78 100644 (file)
@@ -1,4 +1,3 @@
-/* crypto/mem_dbg.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
 #include <openssl/buffer.h>
 #include <openssl/bio.h>
 #include <openssl/lhash.h>
-#if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__)
+
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
 # include <execinfo.h>
 #endif
 
@@ -172,7 +172,7 @@ struct mem_st {
     unsigned long order;
     time_t time;
     APP_INFO *app_info;
-#if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__)
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
     void *array[30];
     size_t array_siz;
 #endif
@@ -331,29 +331,31 @@ static unsigned long app_info_hash(const APP_INFO *a)
     return (ret);
 }
 
-static APP_INFO *pop_info(void)
+/* returns 1 if there was an info to pop, 0 if the stack was empty. */
+static int pop_info(void)
 {
     APP_INFO tmp;
-    APP_INFO *ret = NULL;
+    APP_INFO *current = NULL;
 
     if (amih != NULL) {
         CRYPTO_THREADID_current(&tmp.threadid);
-        if ((ret = lh_APP_INFO_delete(amih, &tmp)) != NULL) {
-            APP_INFO *next = ret->next;
+        if ((current = lh_APP_INFO_delete(amih, &tmp)) != NULL) {
+            APP_INFO *next = current->next;
 
             if (next != NULL) {
                 next->references++;
                 (void)lh_APP_INFO_insert(amih, next);
             }
-            if (--(ret->references) <= 0) {
-                ret->next = NULL;
+            if (--(current->references) <= 0) {
+                current->next = NULL;
                 if (next != NULL)
                     next->references--;
-                OPENSSL_free(ret);
+                OPENSSL_free(current);
             }
+            return 1;
         }
     }
-    return (ret);
+    return 0;
 }
 
 int CRYPTO_mem_debug_push(const char *info, const char *file, int line)
@@ -396,7 +398,7 @@ int CRYPTO_mem_debug_pop(void)
 
     if (mem_check_on()) {
         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
-        ret = (pop_info() != NULL);
+        ret = pop_info();
         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
     }
     return (ret);
@@ -444,7 +446,7 @@ void CRYPTO_mem_debug_malloc(void *addr, size_t num, int before_p,
                 m->order = order;
             }
             m->order = order++;
-# if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__)
+# ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
             m->array_siz = backtrace(m->array, OSSL_NELEM(m->array));
 # endif
             m->time = time(NULL);
@@ -472,7 +474,8 @@ void CRYPTO_mem_debug_malloc(void *addr, size_t num, int before_p,
     return;
 }
 
-void CRYPTO_mem_debug_free(void *addr, int before_p)
+void CRYPTO_mem_debug_free(void *addr, int before_p,
+        const char *file, int line)
 {
     MEM m, *mp;
 
@@ -524,7 +527,7 @@ void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num,
             if (mp != NULL) {
                 mp->addr = addr2;
                 mp->num = num;
-#if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__)
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
                 mp->array_siz = backtrace(mp->array, OSSL_NELEM(mp->array));
 #endif
                 (void)lh_MEM_insert(mh, mp);
@@ -544,7 +547,7 @@ typedef struct mem_leak_st {
     long bytes;
 } MEM_LEAK;
 
-static void print_leak_doall_arg(const MEM *m, MEM_LEAK *l)
+static void print_leak(const MEM *m, MEM_LEAK *l)
 {
     char buf[1024];
     char *bufp = buf;
@@ -617,7 +620,7 @@ static void print_leak_doall_arg(const MEM *m, MEM_LEAK *l)
         while (amip && !CRYPTO_THREADID_cmp(&amip->threadid, &ti));
     }
 
-#if defined(CRYPTO_MDEBUG_BACKTRACE) && defined(__GNUC__)
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
     {
         size_t i;
         char **strings = backtrace_symbols(m->array, m->array_siz);
@@ -629,7 +632,7 @@ static void print_leak_doall_arg(const MEM *m, MEM_LEAK *l)
 #endif
 }
 
-static IMPLEMENT_LHASH_DOALL_ARG_FN(print_leak, const MEM, MEM_LEAK)
+IMPLEMENT_LHASH_DOALL_ARG_CONST(MEM, MEM_LEAK);
 
 int CRYPTO_mem_leaks(BIO *b)
 {
@@ -638,6 +641,9 @@ int CRYPTO_mem_leaks(BIO *b)
     if (mh == NULL && amih == NULL)
         return 1;
 
+    /* Ensure all resources are released */
+    OPENSSL_cleanup();
+
     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
 
     ml.bio = b;
@@ -645,7 +651,7 @@ int CRYPTO_mem_leaks(BIO *b)
     ml.chunks = 0;
     ml.seen = 0;
     if (mh != NULL)
-        lh_MEM_doall_arg(mh, LHASH_DOALL_ARG_FN(print_leak), MEM_LEAK, &ml);
+        lh_MEM_doall_MEM_LEAK(mh, print_leak, &ml);
     /* Don't count the BIO that was passed in as a "leak" */
     if (ml.seen && ml.chunks >= 1 && ml.bytes >= (int)sizeof (*b)) {
         ml.chunks--;
@@ -693,8 +699,8 @@ int CRYPTO_mem_leaks_fp(FILE *fp)
     BIO *b;
     int ret;
 
-    if (mh == NULL)
-        return 0;
+    if (mh == NULL && amih == NULL)
+        return 1;
     /*
      * Need to turn off memory checking when allocated BIOs ... especially as
      * we're creating them at a time when we're trying to check we've not