From 4e482ae6ff62dd992354cad61dd3d81d55b6e204 Mon Sep 17 00:00:00 2001 From: "Dr. Stephen Henson" Date: Sun, 10 Jan 2016 23:25:07 +0000 Subject: [PATCH] Add memory leak return value. Make CRYPTO_mem_leaks() and CRYPTO_mem_leaks_fp() return a status value. Update documentation. Don't abort() if there are leaks. Reviewed-by: Viktor Dukhovni --- crypto/mem_dbg.c | 16 +++++++++------- doc/crypto/OPENSSL_malloc.pod | 11 ++++++----- include/openssl/crypto.h | 4 ++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/crypto/mem_dbg.c b/crypto/mem_dbg.c index 1e9dd83c14..6e93c8047e 100644 --- a/crypto/mem_dbg.c +++ b/crypto/mem_dbg.c @@ -643,12 +643,12 @@ static void print_leak_doall_arg(const MEM *m, MEM_LEAK *l) static IMPLEMENT_LHASH_DOALL_ARG_FN(print_leak, const MEM, MEM_LEAK) -void CRYPTO_mem_leaks(BIO *b) +int CRYPTO_mem_leaks(BIO *b) { MEM_LEAK ml; if (mh == NULL && amih == NULL) - return; + return 1; CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); @@ -665,7 +665,6 @@ void CRYPTO_mem_leaks(BIO *b) } if (ml.chunks != 0) { BIO_printf(b, "%ld bytes leaked in %d chunks\n", ml.bytes, ml.chunks); - abort(); } else { /* * Make sure that, if we found no leaks, memory-leak debugging itself @@ -697,15 +696,17 @@ void CRYPTO_mem_leaks(BIO *b) CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC); } CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); + return ml.chunks == 0 ? 1 : 0; } # ifndef OPENSSL_NO_STDIO -void CRYPTO_mem_leaks_fp(FILE *fp) +int CRYPTO_mem_leaks_fp(FILE *fp) { BIO *b; + int ret; if (mh == NULL) - return; + return 0; /* * 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 @@ -715,10 +716,11 @@ void CRYPTO_mem_leaks_fp(FILE *fp) b = BIO_new(BIO_s_file()); CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); if (b == NULL) - return; + return -1; BIO_set_fp(b, fp, BIO_NOCLOSE); - CRYPTO_mem_leaks(b); + ret = CRYPTO_mem_leaks(b); BIO_free(b); + return ret; } # endif diff --git a/doc/crypto/OPENSSL_malloc.pod b/doc/crypto/OPENSSL_malloc.pod index eef8c64393..9dfeb390bf 100644 --- a/doc/crypto/OPENSSL_malloc.pod +++ b/doc/crypto/OPENSSL_malloc.pod @@ -132,17 +132,18 @@ OPENSSL_mem_debug_pop() removes identifying state from the stack. At the end of the program, calling CRYPTO_mem_leaks() or CRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it -to the specified BIO B or FILE B. -It will then L if there were any unfree'd allocations. +to the specified BIO B or FILE B. These functions return 1 if +there are no leaks, 0 if there are leaks and -1 if an error occurred. =head1 RETURN VALUES OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free() -CRYPTO_free(), CRYPTO_clear_free(), -CRYPTO_get_mem_functions(), and -CRYPTO_mem_leaks() +CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions() return no value. +CRYPTO_mem_leaks() and CRYPTO_mem_leaks_fp() return 1 if there +are no leaks, 0 if there are leaks and -1 if an error occurred. + OPENSSL_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(), OPENSSL_clear_realloc(), CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(), diff --git a/include/openssl/crypto.h b/include/openssl/crypto.h index 5de0f504db..7a45699a62 100644 --- a/include/openssl/crypto.h +++ b/include/openssl/crypto.h @@ -509,9 +509,9 @@ void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num, int flag, void CRYPTO_mem_debug_free(void *addr, int flag); # ifndef OPENSSL_NO_STDIO -void CRYPTO_mem_leaks_fp(FILE *); +int CRYPTO_mem_leaks_fp(FILE *); # endif -void CRYPTO_mem_leaks(struct bio_st *bio); +int CRYPTO_mem_leaks(struct bio_st *bio); # endif /* die if we have to */ -- 2.25.1