From 222ceb7a82187cc758413f028e3e424c70a7d446 Mon Sep 17 00:00:00 2001 From: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> Date: Tue, 19 Sep 2023 13:59:47 +0200 Subject: [PATCH] Fix allocation size overflow check in `CowData` (cherry picked from commit c48b189e149e4940f7cd0ef7f3b77dbc185f2103) --- core/templates/cowdata.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index e33822fedfb..c37d46956df 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -90,6 +90,10 @@ private: } _FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const { + if (unlikely(p_elements == 0)) { + *out = 0; + return true; + } #if defined(__GNUC__) size_t o; size_t p; @@ -101,13 +105,12 @@ private: if (__builtin_add_overflow(o, static_cast(32), &p)) { return false; // No longer allocated here. } - return true; #else // Speed is more important than correctness here, do the operations unchecked // and hope for the best. *out = _get_alloc_size(p_elements); - return true; #endif + return *out; } void _unref(void *p_data);