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 <viktor@openssl.org>
This commit is contained in:
Dr. Stephen Henson 2016-01-10 23:25:07 +00:00
parent c2e27310c7
commit 4e482ae6ff
3 changed files with 17 additions and 14 deletions

View File

@ -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) 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; MEM_LEAK ml;
if (mh == NULL && amih == NULL) if (mh == NULL && amih == NULL)
return; return 1;
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
@ -665,7 +665,6 @@ void CRYPTO_mem_leaks(BIO *b)
} }
if (ml.chunks != 0) { if (ml.chunks != 0) {
BIO_printf(b, "%ld bytes leaked in %d chunks\n", ml.bytes, ml.chunks); BIO_printf(b, "%ld bytes leaked in %d chunks\n", ml.bytes, ml.chunks);
abort();
} else { } else {
/* /*
* Make sure that, if we found no leaks, memory-leak debugging itself * 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_w_unlock(CRYPTO_LOCK_MALLOC);
} }
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
return ml.chunks == 0 ? 1 : 0;
} }
# ifndef OPENSSL_NO_STDIO # ifndef OPENSSL_NO_STDIO
void CRYPTO_mem_leaks_fp(FILE *fp) int CRYPTO_mem_leaks_fp(FILE *fp)
{ {
BIO *b; BIO *b;
int ret;
if (mh == NULL) if (mh == NULL)
return; return 0;
/* /*
* Need to turn off memory checking when allocated BIOs ... especially as * 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 * 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()); b = BIO_new(BIO_s_file());
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
if (b == NULL) if (b == NULL)
return; return -1;
BIO_set_fp(b, fp, BIO_NOCLOSE); BIO_set_fp(b, fp, BIO_NOCLOSE);
CRYPTO_mem_leaks(b); ret = CRYPTO_mem_leaks(b);
BIO_free(b); BIO_free(b);
return ret;
} }
# endif # endif

View File

@ -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 At the end of the program, calling CRYPTO_mem_leaks() or
CRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it CRYPTO_mem_leaks_fp() will report all "leaked" memory, writing it
to the specified BIO B<b> or FILE B<fp>. to the specified BIO B<b> or FILE B<fp>. These functions return 1 if
It will then L<abort(3)> if there were any unfree'd allocations. there are no leaks, 0 if there are leaks and -1 if an error occurred.
=head1 RETURN VALUES =head1 RETURN VALUES
OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free() OPENSSL_malloc_init(), OPENSSL_free(), OPENSSL_clear_free()
CRYPTO_free(), CRYPTO_clear_free(), CRYPTO_free(), CRYPTO_clear_free() and CRYPTO_get_mem_functions()
CRYPTO_get_mem_functions(), and
CRYPTO_mem_leaks()
return no value. 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_malloc(), OPENSSL_zalloc(), OPENSSL_realloc(),
OPENSSL_clear_realloc(), OPENSSL_clear_realloc(),
CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(), CRYPTO_malloc(), CRYPTO_zalloc(), CRYPTO_realloc(),

View File

@ -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); void CRYPTO_mem_debug_free(void *addr, int flag);
# ifndef OPENSSL_NO_STDIO # ifndef OPENSSL_NO_STDIO
void CRYPTO_mem_leaks_fp(FILE *); int CRYPTO_mem_leaks_fp(FILE *);
# endif # endif
void CRYPTO_mem_leaks(struct bio_st *bio); int CRYPTO_mem_leaks(struct bio_st *bio);
# endif # endif
/* die if we have to */ /* die if we have to */