void *CRYPTO_aligned_alloc(size_t num, size_t alignment, void **freeptr,
const char *file, int line)
{
+ size_t alloc_bytes;
void *ret;
*freeptr = NULL;
* via _aligned_malloc, just avoid its use entirely
*/
+ if (ossl_unlikely(!ossl_size_add(num, alignment, &alloc_bytes, file, line)))
+ return NULL;
+
/*
* Step 1: Allocate an amount of memory that is <alignment>
* bytes bigger than requested
*/
- *freeptr = CRYPTO_malloc(num + alignment, file, line);
+ *freeptr = CRYPTO_malloc(alloc_bytes, file, line);
if (*freeptr == NULL)
return NULL;
/*
* Check the result of size1 and size2 addition for overflow
- * and set error if it is the case.
+ * and set error if it is the case; returns true if there was no overflow,
+ * false if there was.
*/
static ossl_inline ossl_unused bool
-ossl_size_add_of(const size_t size1, const size_t size2, size_t *bytes,
- const char * const file, const int line)
+ossl_size_add(const size_t size1, const size_t size2, size_t *bytes,
+ const char * const file, const int line)
{
- *bytes = size1 + size2;
+ int err = 0;
+ *bytes = safe_add_size_t(size1, size2, &err);
- if (ossl_unlikely(*bytes < size1)) {
+ if (ossl_unlikely(err != 0)) {
ossl_report_alloc_err_of(file, line);
- return true;
+ return false;
}
- return false;
+ return true;
}
#endif /* OSSL_INTERNAL_CHECK_SIZE_OVERFLOW_H */