From 9a380ff199eafc35f59d8b231cc7e2757b62ff12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Thu, 19 Sep 2024 14:09:05 +0200 Subject: [PATCH] Do not add snap item if extended item type could not be added The `CSnapshotBuilder::AddExtendedItemType` function can fail if the snapshot is already full. Previously, this was not handled, leading to `m_aExtendedItemTypes` being updated inconsistently with the real extended item types and the snap item being added without the extended item type. Now, the snap item is not added if its extended item type could not be added. --- src/engine/shared/snapshot.cpp | 39 ++++++++++++++++++++++------------ src/engine/shared/snapshot.h | 2 +- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/engine/shared/snapshot.cpp b/src/engine/shared/snapshot.cpp index 0e2b777f8..5d1222357 100644 --- a/src/engine/shared/snapshot.cpp +++ b/src/engine/shared/snapshot.cpp @@ -767,17 +767,22 @@ int CSnapshotBuilder::GetTypeFromIndex(int Index) const return CSnapshot::MAX_TYPE - Index; } -void CSnapshotBuilder::AddExtendedItemType(int Index) +bool CSnapshotBuilder::AddExtendedItemType(int Index) { dbg_assert(0 <= Index && Index < m_NumExtendedItemTypes, "index out of range"); - int TypeId = m_aExtendedItemTypes[Index]; - CUuid Uuid = g_UuidManager.GetUuid(TypeId); - int *pUuidItem = (int *)NewItem(0, GetTypeFromIndex(Index), sizeof(Uuid)); // NETOBJTYPE_EX - if(pUuidItem) + int *pUuidItem = static_cast(NewItem(0, GetTypeFromIndex(Index), sizeof(CUuid))); // NETOBJTYPE_EX + if(pUuidItem == nullptr) { - for(size_t i = 0; i < sizeof(CUuid) / sizeof(int32_t); i++) - pUuidItem[i] = bytes_be_to_uint(&Uuid.m_aData[i * sizeof(int32_t)]); + return false; } + + const int TypeId = m_aExtendedItemTypes[Index]; + const CUuid Uuid = g_UuidManager.GetUuid(TypeId); + for(size_t i = 0; i < sizeof(CUuid) / sizeof(int32_t); i++) + { + pUuidItem[i] = bytes_be_to_uint(&Uuid.m_aData[i * sizeof(int32_t)]); + } + return true; } int CSnapshotBuilder::GetExtendedItemTypeIndex(int TypeId) @@ -793,8 +798,12 @@ int CSnapshotBuilder::GetExtendedItemTypeIndex(int TypeId) int Index = m_NumExtendedItemTypes; m_NumExtendedItemTypes++; m_aExtendedItemTypes[Index] = TypeId; - AddExtendedItemType(Index); - return Index; + if(AddExtendedItemType(Index)) + { + return Index; + } + m_NumExtendedItemTypes--; + return -1; } void *CSnapshotBuilder::NewItem(int Type, int Id, int Size) @@ -816,11 +825,15 @@ void *CSnapshotBuilder::NewItem(int Type, int Id, int Size) return nullptr; } - bool Extended = false; - if(Type >= OFFSET_UUID) + const bool Extended = Type >= OFFSET_UUID; + if(Extended) { - Extended = true; - Type = GetTypeFromIndex(GetExtendedItemTypeIndex(Type)); + const int ExtendedItemTypeIndex = GetExtendedItemTypeIndex(Type); + if(ExtendedItemTypeIndex == -1) + { + return nullptr; + } + Type = GetTypeFromIndex(ExtendedItemTypeIndex); } CSnapshotItem *pObj = (CSnapshotItem *)(m_aData + m_DataSize); diff --git a/src/engine/shared/snapshot.h b/src/engine/shared/snapshot.h index 24aeaafa9..7a001221d 100644 --- a/src/engine/shared/snapshot.h +++ b/src/engine/shared/snapshot.h @@ -158,7 +158,7 @@ class CSnapshotBuilder int m_aExtendedItemTypes[MAX_EXTENDED_ITEM_TYPES]; int m_NumExtendedItemTypes; - void AddExtendedItemType(int Index); + bool AddExtendedItemType(int Index); int GetExtendedItemTypeIndex(int TypeId); int GetTypeFromIndex(int Index) const;