diff options
author | Dr. David von Oheimb <David.von.Oheimb@siemens.com> | 2021-04-12 10:01:51 +0200 |
---|---|---|
committer | Dr. David von Oheimb <dev@ddvo.net> | 2022-08-27 09:40:09 +0200 |
commit | 5639ee79bdc905c1b7ed308fcd29e515e4f01a50 (patch) | |
tree | 1244577a1904367b1886884cd0d590e99459ae0e /crypto/mem.c | |
parent | 555dd9390ba56f1c400d3f067a2dfe7b00fbf7d3 (diff) |
ERR: Make CRYPTO_malloc() and friends report ERR_R_MALLOC_FAILURE
Fixes #6251
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
(Merged from https://github.com/openssl/openssl/pull/14833)
Diffstat (limited to 'crypto/mem.c')
-rw-r--r-- | crypto/mem.c | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/crypto/mem.c b/crypto/mem.c index 3d67d9256a..74bf3b892c 100644 --- a/crypto/mem.c +++ b/crypto/mem.c @@ -170,9 +170,15 @@ void ossl_malloc_setup_failures(void) void *CRYPTO_malloc(size_t num, const char *file, int line) { + void *ptr; + INCREMENT(malloc_count); - if (malloc_impl != CRYPTO_malloc) - return malloc_impl(num, file, line); + if (malloc_impl != CRYPTO_malloc) { + ptr = malloc_impl(num, file, line); + if (ptr != NULL || num == 0) + return ptr; + goto err; + } if (num == 0) return NULL; @@ -187,7 +193,20 @@ void *CRYPTO_malloc(size_t num, const char *file, int line) allow_customize = 0; } - return malloc(num); + ptr = malloc(num); + if (ptr != NULL) + return ptr; + err: + /* + * ossl_err_get_state_int() in err.c uses CRYPTO_zalloc(num, NULL, 0) for + * ERR_STATE allocation. Prevent mem alloc error loop while reporting error. + */ + if (file != NULL || line != 0) { + ERR_new(); + ERR_set_debug(file, line, NULL); + ERR_set_error(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE, NULL); + } + return NULL; } void *CRYPTO_zalloc(size_t num, const char *file, int line) |