diff --git a/src/dbc/Lookup.cpp b/src/dbc/Lookup.cpp index 57674977..9458f22c 100644 --- a/src/dbc/Lookup.cpp +++ b/src/dbc/Lookup.cpp @@ -25,11 +25,13 @@ const char *NormalizeEmpty(const char *s) { } // namespace +// The id is compared unsigned: a DBC column's "none" is -1, which a signed +// compare passes as less than the count and then indexes records[-1]. The +// engine rejects it with a signed `jl` before its `jg` against the count +// (0x00472797). An unloaded store's count is -1, so it takes no id. const uint8_t *Record(uintptr_t recordsVar, uintptr_t countVar, uint32_t id) { - if (id == 0) - return nullptr; const int count = *reinterpret_cast(countVar); - if (static_cast(id) > count) + if (id == 0 || count <= 0 || id > static_cast(count)) return nullptr; const uint8_t *const *records = *reinterpret_cast(recordsVar); diff --git a/src/dbc/Lookup.h b/src/dbc/Lookup.h index c3204d9f..0f3fa655 100644 --- a/src/dbc/Lookup.h +++ b/src/dbc/Lookup.h @@ -39,7 +39,8 @@ namespace DBC { // Resolves `id` to its record pointer in the table whose records-base // global is at `recordsVar` and count global at `countVar`. Returns -// `nullptr` for `id == 0`, OOR ids (> count), and unpopulated slots. +// `nullptr` for `id == 0`, OOR ids (> count, which includes -1 and every +// other negative id), and unpopulated slots. // Caller reads whatever fields it needs off the returned record. // // Use this when `LocalizedField` / `StringField` don't fit — e.g. for diff --git a/src/pvp/Arena.cpp b/src/pvp/Arena.cpp index 79de653f..e24a9933 100644 --- a/src/pvp/Arena.cpp +++ b/src/pvp/Arena.cpp @@ -13,6 +13,7 @@ #include "Game.h" #include "Offsets.h" +#include "dbc/Lookup.h" #include @@ -46,17 +47,8 @@ namespace { constexpr uint32_t MAP_INSTANCE_TYPE_PVP = 3; -const uint8_t *DbcRecord(uintptr_t recordsVar, uintptr_t countVar, uint32_t id) { - const int count = *reinterpret_cast(countVar); - if (id == 0 || static_cast(id) > count) - return nullptr; - const uint8_t *const *records = - *reinterpret_cast(recordsVar); - return records != nullptr ? records[id] : nullptr; -} - bool IsArenaArea(uint32_t areaID) { - const uint8_t *rec = DbcRecord(Offsets::VAR_AREATABLE_RECORDS, + const uint8_t *rec = DBC::Record(Offsets::VAR_AREATABLE_RECORDS, Offsets::VAR_AREATABLE_COUNT, areaID); if (rec == nullptr) return false; @@ -69,7 +61,7 @@ bool InArena() { const uint32_t mapID = *reinterpret_cast( static_cast(Offsets::VAR_CURRENT_MAP_ID)); const uint8_t *map = - DbcRecord(Offsets::VAR_MAP_RECORDS, Offsets::VAR_MAP_COUNT, mapID); + DBC::Record(Offsets::VAR_MAP_RECORDS, Offsets::VAR_MAP_COUNT, mapID); if (map == nullptr) return false; const uint32_t type =