Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix irrString use-after-free with char-like assignment (operator=) #15213

Merged
Merged
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions irr/include/irrString.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <cstdio>
#include <cstring>
#include <cwchar>
#include <type_traits>

/* HACK: import these string methods from MT's util/string.h */
extern std::wstring utf8_to_wide(std::string_view input);
Expand Down Expand Up @@ -165,21 +166,25 @@ class string
}

//! Assignment operator for strings, ASCII and Unicode
template <class B>
string<T> &operator=(const B *const c)
template <class B, std::enable_if_t<std::is_array_v<B[]>, bool> = true>
string<T> &operator=(const B * c)
{
if (!c) {
clear();
return *this;
}

// no longer allowed!
_IRR_DEBUG_BREAK_IF((void *)c == (void *)c_str());

u32 len = calclen(c);
str.resize(len);
for (u32 l = 0; l < len; ++l)
str[l] = (T)c[l];

_IRR_DEBUG_BREAK_IF(
reinterpret_cast<uintptr_t>(c) >= (uintptr_t)(str.data()) &&
reinterpret_cast<uintptr_t>(c) < (uintptr_t)(str.data()) + str.size());

u32 len = calclen(c);
// In case `c` is a pointer to our own buffer, we may not resize first
// or it can become invalid.
if (len > str.size()) str.resize(len);
for (u32 l = 0; l < len; ++l)
str[l] = static_cast<T>(c[l]);
if (len < str.size()) str.resize(len);

return *this;
}
Expand Down
Loading