Codechange: Don't use cpp_offsetof in the TTO/TTD savegame loader.

Many of the member variables that are used in the oldloader are inside types
that are not standard layout types. Using pointer arithmetics to determine
addresses of members inside types that are not standard layout is generally
undefined behaviour. If we'd use C++17, it is conditionally supported, which means
each compiler may or may not support it. And even then using it for individual
array elements is syntactically not supported the the standard offsetof function.
This commit is contained in:
Michael Lutz
2020-12-14 00:11:39 +01:00
parent 0f621b4956
commit 9c9292949f
2 changed files with 14 additions and 15 deletions

View File

@@ -108,8 +108,6 @@ byte ReadByte(LoadgameState *ls)
*/
bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
{
byte *base_ptr = (byte*)base;
for (const OldChunks *chunk = chunks; chunk->type != OC_END; chunk++) {
if (((chunk->type & OC_TTD) && _savegame_type == SGT_TTO) ||
((chunk->type & OC_TTO) && _savegame_type != SGT_TTO)) {
@@ -134,8 +132,8 @@ bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
break;
case OC_ASSERT:
DEBUG(oldloader, 4, "Assert point: 0x%X / 0x%X", ls->total_read, chunk->offset + _bump_assert_value);
if (ls->total_read != chunk->offset + _bump_assert_value) throw std::exception();
DEBUG(oldloader, 4, "Assert point: 0x%X / 0x%X", ls->total_read, (uint)(size_t)chunk->ptr + _bump_assert_value);
if (ls->total_read != (size_t)chunk->ptr + _bump_assert_value) throw std::exception();
default: break;
}
} else {
@@ -153,10 +151,10 @@ bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
}
/* When both pointers are nullptr, we are just skipping data */
if (base_ptr == nullptr && chunk->ptr == nullptr) continue;
if (base == nullptr && chunk->ptr == nullptr) continue;
/* Writing to the var: bits 8 to 15 have the VAR type */
if (chunk->ptr == nullptr) ptr = base_ptr + chunk->offset;
/* Chunk refers to a struct member, get address in base. */
if (chunk->ptr == nullptr) ptr = (byte *)chunk->offset(base);
/* Write the data */
switch (GetOldChunkVarType(chunk->type)) {