Fixed stupid buffer overflow in array setblocks

This commit is contained in:
Tycho 2014-05-10 17:46:49 +01:00
parent ba25f6b524
commit 0adb5c94b8
3 changed files with 13 additions and 13 deletions

View File

@ -35,7 +35,7 @@ void cChunkBuffer::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length
memcpy( memcpy(
&a_dest[i * segment_length], &a_dest[i * segment_length],
&m_Sections[i]->m_BlockTypes, &m_Sections[i]->m_BlockTypes,
sizeof(BLOCKTYPE) * length sizeof(BLOCKTYPE) * tocopy
); );
} }
else else
@ -43,7 +43,7 @@ void cChunkBuffer::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length
memset( memset(
&a_dest[i * segment_length], &a_dest[i * segment_length],
0, 0,
sizeof(BLOCKTYPE) * length sizeof(BLOCKTYPE) * tocopy
); );
} }
} }
@ -141,7 +141,7 @@ void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src)
{ {
for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
{ {
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16;
if (m_Sections[i]) if (m_Sections[i])
{ {
memcpy(&m_Sections[i]->m_BlockTypes, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length); memcpy(&m_Sections[i]->m_BlockTypes, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length);

View File

@ -124,7 +124,7 @@ public:
m_Sections[Section] = Allocate(); m_Sections[Section] = Allocate();
if(!m_Sections[Section]) if(!m_Sections[Section])
{ {
ASSERT("Failed to allocate a new section in Chunkbuffer"); ASSERT(!"Failed to allocate a new section in Chunkbuffer");
return; return;
} }
} }
@ -169,7 +169,7 @@ public:
m_Sections[Section] = Allocate(); m_Sections[Section] = Allocate();
if(!m_Sections[Section]) if(!m_Sections[Section])
{ {
ASSERT("Failed to allocate a new section in Chunkbuffer"); ASSERT(!"Failed to allocate a new section in Chunkbuffer");
return; return;
} }
} }

View File

@ -15,8 +15,8 @@ int main(int argc, char** argv)
testassert(copy.GetBlock(3,1,4) == 0xDE); testassert(copy.GetBlock(3,1,4) == 0xDE);
testassert(copy.GetMeta(3,1,4) == 0xA); testassert(copy.GetMeta(3,1,4) == 0xA);
BLOCKTYPE * SrcBlockBuffer = new BLOCKTYPE[256 * 256 * 256]; BLOCKTYPE * SrcBlockBuffer = new BLOCKTYPE[16 * 16 * 256];
for (int i = 0; i < 256* 256 * 256; i += 4) for (int i = 0; i < 16 * 16 * 256; i += 4)
{ {
SrcBlockBuffer[i+0] = 0xDE; SrcBlockBuffer[i+0] = 0xDE;
SrcBlockBuffer[i+1] = 0xAD; SrcBlockBuffer[i+1] = 0xAD;
@ -25,16 +25,16 @@ int main(int argc, char** argv)
} }
buffer.SetBlocks(SrcBlockBuffer); buffer.SetBlocks(SrcBlockBuffer);
BLOCKTYPE * DstBlockBuffer = new BLOCKTYPE[256 * 256 * 256]; BLOCKTYPE * DstBlockBuffer = new BLOCKTYPE[16 * 16 * 256];
buffer.CopyBlocks(DstBlockBuffer); buffer.CopyBlocks(DstBlockBuffer);
testassert(memcmp(SrcBlockBuffer, DstBlockBuffer, (256 * 256 * 256) -1) == 0); testassert(memcmp(SrcBlockBuffer, DstBlockBuffer, (16 * 16 * 256) -1) == 0);
delete SrcBlockBuffer; delete SrcBlockBuffer;
delete DstBlockBuffer; delete DstBlockBuffer;
SrcBlockBuffer = NULL; SrcBlockBuffer = NULL;
DstBlockBuffer = NULL; DstBlockBuffer = NULL;
NIBBLETYPE * SrcNibbleBuffer = new NIBBLETYPE[256 * 256 * 256/2]; NIBBLETYPE * SrcNibbleBuffer = new NIBBLETYPE[16 * 16 * 256/2];
for (int i = 0; i < 256* 256 * 256 / 2; i += 4) for (int i = 0; i < 16 * 16 * 256 / 2; i += 4)
{ {
SrcNibbleBuffer[i+0] = 0xEF; SrcNibbleBuffer[i+0] = 0xEF;
SrcNibbleBuffer[i+1] = 0xDE; SrcNibbleBuffer[i+1] = 0xDE;
@ -43,9 +43,9 @@ int main(int argc, char** argv)
} }
buffer.SetMeta(SrcNibbleBuffer); buffer.SetMeta(SrcNibbleBuffer);
NIBBLETYPE * DstNibbleBuffer = new NIBBLETYPE[256 * 256 * 256/ 2]; NIBBLETYPE * DstNibbleBuffer = new NIBBLETYPE[16 * 16 * 256/ 2];
buffer.CopyMeta(DstNibbleBuffer); buffer.CopyMeta(DstNibbleBuffer);
testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (256 * 256 * 256 /2) -1) == 0); testassert(memcmp(SrcNibbleBuffer, DstNibbleBuffer, (16 * 16 * 256 /2) -1) == 0);
delete SrcNibbleBuffer; delete SrcNibbleBuffer;
delete DstNibbleBuffer; delete DstNibbleBuffer;
SrcNibbleBuffer = NULL; SrcNibbleBuffer = NULL;