Skip to content

Commit a3dfbbd

Browse files
authored
Merge pull request #1245 from AThousandShips/alloc_fix
Fix allocation size overflow check in `CowData`
2 parents b1fd1b6 + 06ffc7e commit a3dfbbd

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

include/godot_cpp/templates/cowdata.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ class CowData {
102102
}
103103

104104
_FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const {
105+
if (unlikely(p_elements == 0)) {
106+
*out = 0;
107+
return true;
108+
}
105109
#if defined(__GNUC__)
106110
size_t o;
107111
size_t p;
@@ -113,13 +117,12 @@ class CowData {
113117
if (__builtin_add_overflow(o, static_cast<size_t>(32), &p)) {
114118
return false; // No longer allocated here.
115119
}
116-
return true;
117120
#else
118121
// Speed is more important than correctness here, do the operations unchecked
119122
// and hope for the best.
120123
*out = _get_alloc_size(p_elements);
121-
return true;
122124
#endif
125+
return *out;
123126
}
124127

125128
void _unref(void *p_data);

0 commit comments

Comments
 (0)