From 8cdabb890f2967d74e801ff090beff454322a81b Mon Sep 17 00:00:00 2001 From: oy Date: Tue, 18 Jan 2011 18:09:09 +0100 Subject: [PATCH] fixed that the server crashes on too many snap items (Closes #317), optimised snapshot hashing --- src/engine/shared/snapshot.cpp | 15 ++++++++++----- src/engine/shared/snapshot.h | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/engine/shared/snapshot.cpp b/src/engine/shared/snapshot.cpp index ac16a6f8e..7ead43201 100644 --- a/src/engine/shared/snapshot.cpp +++ b/src/engine/shared/snapshot.cpp @@ -80,7 +80,7 @@ static void GenerateHash(CItemList *pHashlist, CSnapshot *pSnapshot) for(int i = 0; i < pSnapshot->NumItems(); i++) { int Key = pSnapshot->GetItem(i)->Key(); - int HashID = ((Key>>8)&0xf0) | (Key&0xf); + int HashID = ((Key>>12)&0xf0) | (Key&0xf); if(pHashlist[HashID].m_Num != 64) { pHashlist[HashID].m_aIndex[pHashlist[HashID].m_Num] = i; @@ -92,7 +92,7 @@ static void GenerateHash(CItemList *pHashlist, CSnapshot *pSnapshot) static int GetItemIndexHashed(int Key, const CItemList *pHashlist) { - int HashID = ((Key>>8)&0xf0) | (Key&0xf); + int HashID = ((Key>>12)&0xf0) | (Key&0xf); for(int i = 0; i < pHashlist[HashID].m_Num; i++) { if(pHashlist[HashID].m_aKeys[i] == Key) @@ -523,6 +523,14 @@ int CSnapshotBuilder::Finish(void *SpnapData) void *CSnapshotBuilder::NewItem(int Type, int ID, int Size) { + if(m_DataSize + sizeof(CSnapshotItem) + Size >= CSnapshot::MAX_SIZE || + m_NumItems+1 >= MAX_ITEMS) + { + dbg_assert(m_DataSize < CSnapshot::MAX_SIZE, "too much data"); + dbg_assert(m_NumItems < MAX_ITEMS, "too many items"); + return 0; + } + CSnapshotItem *pObj = (CSnapshotItem *)(m_aData + m_DataSize); mem_zero(pObj, sizeof(CSnapshotItem) + Size); @@ -530,9 +538,6 @@ void *CSnapshotBuilder::NewItem(int Type, int ID, int Size) m_aOffsets[m_NumItems] = m_DataSize; m_DataSize += sizeof(CSnapshotItem) + Size; m_NumItems++; - - dbg_assert(m_DataSize < CSnapshot::MAX_SIZE, "too much data"); - dbg_assert(m_NumItems < MAX_ITEMS, "too many items"); return pObj->Data(); } diff --git a/src/engine/shared/snapshot.h b/src/engine/shared/snapshot.h index abd25a72f..34c3a5487 100644 --- a/src/engine/shared/snapshot.h +++ b/src/engine/shared/snapshot.h @@ -114,7 +114,7 @@ class CSnapshotBuilder { enum { - MAX_ITEMS = 1024*2 + MAX_ITEMS = 1024 }; char m_aData[CSnapshot::MAX_SIZE];