crypto/mem: factor out memory allocation failure reporting

Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org>
This commit is contained in:
Eugene Syromiatnikov 2025-07-17 03:29:35 +02:00
parent 5d0c6c52e7
commit a2f857061d
1 changed files with 14 additions and 9 deletions

View File

@ -183,6 +183,19 @@ void ossl_malloc_setup_failures(void)
} }
#endif #endif
static inline void report_alloc_err(const char *file, int line)
{
/*
* 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);
}
}
void *CRYPTO_malloc(size_t num, const char *file, int line) void *CRYPTO_malloc(size_t num, const char *file, int line)
{ {
void *ptr; void *ptr;
@ -212,15 +225,7 @@ void *CRYPTO_malloc(size_t num, const char *file, int line)
if (ossl_likely(ptr != NULL)) if (ossl_likely(ptr != NULL))
return ptr; return ptr;
err: err:
/* report_alloc_err(file, line);
* 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; return NULL;
} }