Skip to content

Commit 652a9e6

Browse files
IvorforceSplizard
authored andcommitted
Revert regression (godotengineGH-31736) of memory unsafe append_array (append vector to itself). Add comments to prevent future regressions.
1 parent d3402f7 commit 652a9e6

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

core/templates/local_vector.h

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class LocalVector {
5757
return data;
5858
}
5959

60+
// Must take a copy instead of a reference (see GH-31736).
6061
_FORCE_INLINE_ void push_back(T p_elem) {
6162
if (unlikely(count == capacity)) {
6263
capacity = tight ? (capacity + 1) : MAX((U)1, capacity << 1);

core/templates/vector.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Vector {
7171
CowData<T> _cowdata;
7272

7373
public:
74+
// Must take a copy instead of a reference (see GH-31736).
7475
bool push_back(T p_elem);
7576
_FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias
7677
void fill(T p_elem);
@@ -99,12 +100,14 @@ class Vector {
99100
Error resize(Size p_size) { return _cowdata.resize(p_size); }
100101
Error resize_zeroed(Size p_size) { return _cowdata.template resize<true>(p_size); }
101102
_FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
103+
// Must take a copy instead of a reference (see GH-31736).
102104
Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
103105
Size find(const T &p_val, Size p_from = 0) const { return _cowdata.find(p_val, p_from); }
104106
Size rfind(const T &p_val, Size p_from = -1) const { return _cowdata.rfind(p_val, p_from); }
105107
Size count(const T &p_val) const { return _cowdata.count(p_val); }
106108

107-
void append_array(const Vector<T> &p_other);
109+
// Must take a copy instead of a reference (see GH-31736).
110+
void append_array(Vector<T> p_other);
108111

109112
_FORCE_INLINE_ bool has(const T &p_val) const { return find(p_val) != -1; }
110113

@@ -301,7 +304,7 @@ void Vector<T>::reverse() {
301304
}
302305

303306
template <typename T>
304-
void Vector<T>::append_array(const Vector<T> &p_other) {
307+
void Vector<T>::append_array(Vector<T> p_other) {
305308
const Size ds = p_other.size();
306309
if (ds == 0) {
307310
return;

0 commit comments

Comments
 (0)