Merge pull request #14038 from unknownbrackets/savestate-str

SaveState: Load misaligned wide strings properly
This commit is contained in:
Henrik Rydgård 2021-02-15 18:50:23 +01:00 committed by GitHub
commit 5d62610a93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -111,14 +111,22 @@ void Do(PointerWrap &p, std::string &x) {
}
void Do(PointerWrap &p, std::wstring &x) {
int stringLen = sizeof(wchar_t)*((int)x.length() + 1);
int stringLen = sizeof(wchar_t) * ((int)x.length() + 1);
Do(p, stringLen);
auto read = [&]() {
std::wstring r;
// In case unaligned, use memcpy.
r.resize((stringLen / sizeof(wchar_t)) - 1);
memcpy(&r[0], *p.ptr, stringLen - sizeof(wchar_t));
return r;
};
switch (p.mode) {
case PointerWrap::MODE_READ: x = (wchar_t*)*p.ptr; break;
case PointerWrap::MODE_READ: x = read(); break;
case PointerWrap::MODE_WRITE: memcpy(*p.ptr, x.c_str(), stringLen); break;
case PointerWrap::MODE_MEASURE: break;
case PointerWrap::MODE_VERIFY: _dbg_assert_msg_(x == (wchar_t*)*p.ptr, "Savestate verification failure: \"%ls\" != \"%ls\" (at %p).\n", x.c_str(), (wchar_t*)*p.ptr, p.ptr); break;
case PointerWrap::MODE_VERIFY: _dbg_assert_msg_(x == read(), "Savestate verification failure: \"%ls\" != \"%ls\" (at %p).\n", x.c_str(), read().c_str(), p.ptr); break;
}
(*p.ptr) += stringLen;
}
@ -127,11 +135,19 @@ void Do(PointerWrap &p, std::u16string &x) {
int stringLen = sizeof(char16_t) * ((int)x.length() + 1);
Do(p, stringLen);
auto read = [&]() {
std::u16string r;
// In case unaligned, use memcpy.
r.resize((stringLen / sizeof(char16_t)) - 1);
memcpy(&r[0], *p.ptr, stringLen - sizeof(char16_t));
return r;
};
switch (p.mode) {
case PointerWrap::MODE_READ: x = (char16_t*)*p.ptr; break;
case PointerWrap::MODE_READ: x = read(); break;
case PointerWrap::MODE_WRITE: memcpy(*p.ptr, x.c_str(), stringLen); break;
case PointerWrap::MODE_MEASURE: break;
case PointerWrap::MODE_VERIFY: _dbg_assert_msg_(x == (char16_t*)*p.ptr, "Savestate verification failure: (at %p).\n", x.c_str()); break;
case PointerWrap::MODE_VERIFY: _dbg_assert_msg_(x == read(), "Savestate verification failure: (at %p).\n", p.ptr); break;
}
(*p.ptr) += stringLen;
}