Otherwise the roundup calculation performed in the open-coded implementation
may put the pointer out of bounds.
Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org>
Reviewed-by: Saša Nedvědický <sashan@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/28059)
return NULL;
#endif
+ /* Ensure that alignment is a power of two */
+ if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
+ ossl_report_alloc_err_inv(file, line);
+ return NULL;
+ }
+
/* Allow non-malloc() allocations as long as no malloc_impl is provided. */
if (malloc_impl == CRYPTO_malloc) {
#if defined(_BSD_SOURCE) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
ossl_report_alloc_err_ex(file, line, CRYPTO_R_INTEGER_OVERFLOW);
}
+/* Report invalid memory allocation call arguments. */
+static ossl_inline ossl_unused void
+ossl_report_alloc_err_inv(const char * const file, const int line)
+{
+ ossl_report_alloc_err_ex(file, line, ERR_R_PASSED_INVALID_ARGUMENT);
+}
+
/*
* Check the result of num and size multiplication for overflow
* and set error if it is the case; return true if there was no overflow,