diff options
author | Michael Lutz <michi@icosahedron.de> | 2020-12-14 00:11:39 +0100 |
---|---|---|
committer | Michael Lutz <michi@icosahedron.de> | 2021-02-13 20:08:53 +0100 |
commit | 9c9292949fa4d699c58d40abcac563a86df239dd (patch) | |
tree | 73dc0836d0e4de6c8beab14fa26982df495f070a /src/saveload/oldloader.cpp | |
parent | 0f621b4956b82313923ef28233cf52919f70eca3 (diff) | |
download | openttd-9c9292949fa4d699c58d40abcac563a86df239dd.tar.xz |
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.
Diffstat (limited to 'src/saveload/oldloader.cpp')
-rw-r--r-- | src/saveload/oldloader.cpp | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/src/saveload/oldloader.cpp b/src/saveload/oldloader.cpp index 42970c9a2..6bc07ad27 100644 --- a/src/saveload/oldloader.cpp +++ b/src/saveload/oldloader.cpp @@ -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)) { |