Skip to content

Commit 05cbd84

Browse files
authored
Fix irrString use-after-free with char-like assignment (operator=)
1 parent 84b9321 commit 05cbd84

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

irr/include/irrString.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,24 @@ class string
173173
return *this;
174174
}
175175

176-
// no longer allowed!
177-
_IRR_DEBUG_BREAK_IF((void *)c == (void *)c_str());
176+
if constexpr (sizeof(T) != sizeof(B)) {
177+
_IRR_DEBUG_BREAK_IF(
178+
(uintptr_t)c >= (uintptr_t)(str.data()) &&
179+
(uintptr_t)c < (uintptr_t)(str.data() + str.size()));
180+
}
181+
182+
if ((void *)c == (void *)c_str())
183+
return *this;
178184

179185
u32 len = calclen(c);
180-
str.resize(len);
186+
// In case `c` is a pointer to our own buffer, we may not resize first
187+
// or it can become invalid.
188+
if (len > str.size())
189+
str.resize(len);
181190
for (u32 l = 0; l < len; ++l)
182-
str[l] = (T)c[l];
191+
str[l] = static_cast<T>(c[l]);
192+
if (len < str.size())
193+
str.resize(len);
183194

184195
return *this;
185196
}

0 commit comments

Comments
 (0)