Find openssl memory leaks (code)

#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/crypto.h>

static BIO *bio_err = NULL;

void mem_debug_start()
{
    bio_err = BIO_new(BIO_s_file());
    if (!bio_err) {
        printf("mem_debug_start bio_err ERRPR!\n");
        return;
    }   

    BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
    CRYPTO_malloc_debug_init();
    CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
}

void mem_debug_stop()
{
    if (bio_err) {
        CRYPTO_mem_leaks(bio_err);
        BIO_free(bio_err);
    }   
}

int main(void)
{
    char *p = NULL, *num = "123";

    mem_debug_start();
    
    p = OPENSSL_malloc(4);

    memmove(p, num, strlen(num));

    printf("%s\n", p); 

//     OPENSSL_free(p);
//     p = NULL;

    mem_debug_stop();

    return 0;
}

编译:
gcc mem_debug.c -I . -I ~/openssl/soft/include -lssl -lcrypto -L ~/openssl/soft/lib -ldl

  An error message will appear after running:

123
[16:17:47]     0 file=mem_debug.c, line=36, thread=3077924488, number=4, address=09DA3150

  Because p does not release all there is a memory leak. This is explained in Chapter 5 Memory Allocation in the ssl programming book.

  OpenSSL provides some powerful memory management functions, which can easily check the memory leak points. You can see how much memory leak is caused by a memory application, which is caused by that file and that sentence.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325019704&siteId=291194637