Skip to content

Commit 19db6dd

Browse files
committed
resolve alignment undefined behavior by enabling C++11
1 parent 30ac62f commit 19db6dd

14 files changed

Lines changed: 42 additions & 27 deletions

File tree

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ endif()
5858
set(CMAKE_C_STANDARD 11)
5959
set(CMAKE_C_STANDARD_REQUIRED TRUE)
6060

61+
set(CMAKE_CXX_STANDARD 11)
62+
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
63+
6164
option(CLIENT "Compile client" ON)
6265
option(HEADLESS_CLIENT "Build the client without graphics" OFF)
6366
option(DOWNLOAD_DEPENDENCIES "Download dependencies (only available on Windows)" ${AUTO_DEPENDENCIES_DEFAULT})
@@ -142,6 +145,7 @@ if(NOT MSVC)
142145
add_c_compiler_flag_if_supported(OUR_FLAGS_OWN -Wno-missing-field-initializers)
143146
add_c_compiler_flag_if_supported(OUR_FLAGS_OWN -Wformat=2) # Warn about format strings.
144147
add_c_compiler_flag_if_supported(OUR_FLAGS_OWN -Wno-stringop-truncation)
148+
add_c_compiler_flag_if_supported(OUR_FLAGS_OWN -fno-exceptions)
145149
add_c_compiler_flag_if_supported(OUR_FLAGS_DEP -Wno-implicit-function-declaration)
146150
endif()
147151

bam.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ end
9292

9393
function GenerateCommonSettings(settings, conf, arch, compiler)
9494
if compiler == "gcc" or compiler == "clang" then
95-
settings.cc.flags:Add("-Wall", "-fno-exceptions")
95+
settings.cc.flags:Add("-Wall", "-fno-exceptions", "-std=c++11")
9696
end
9797

9898
-- Compile zlib if needed

src/engine/client/backend_sdl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ bool CCommandProcessorFragment_OpenGL::SetState(const CCommandBuffer::CState &St
292292
// texture handling
293293
bool HasValidTexture = false;
294294
int SrcBlendMode = GL_ONE;
295-
bool IsAlphaOnly = m_aTextures[State.m_Texture].m_Format == CCommandBuffer::TEXFORMAT_ALPHA;
296295
if(m_LastStainedOnly != State.m_IsStainedOnly)
297296
{
298297
glUniform1i(m_RenderShader.m_IsStainedOnlyLoc, State.m_IsStainedOnly ? 1 : 0);
@@ -301,6 +300,7 @@ bool CCommandProcessorFragment_OpenGL::SetState(const CCommandBuffer::CState &St
301300

302301
if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES)
303302
{
303+
bool IsAlphaOnly = m_aTextures[State.m_Texture].m_Format == CCommandBuffer::TEXFORMAT_ALPHA;
304304
if(m_aTextures[State.m_Texture].m_Valid)
305305
{
306306
if(m_LastTextureID != m_aTextures[State.m_Texture].m_Texture)

src/engine/client/serverbrowser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ const char *CServerBrowser::GetFavoritePassword(const char *pAddress)
471471
CServerEntry *CServerBrowser::Add(int ServerlistType, const NETADDR &Addr)
472472
{
473473
// create new pEntry
474-
CServerEntry *pEntry = (CServerEntry *) m_aServerlist[ServerlistType].m_ServerlistHeap.Allocate(sizeof(CServerEntry));
474+
CServerEntry *pEntry = (CServerEntry *) m_aServerlist[ServerlistType].m_ServerlistHeap.Allocate(sizeof(CServerEntry), alignof(CServerEntry));
475475
mem_zero(pEntry, sizeof(CServerEntry));
476476

477477
// set the info

src/engine/client/textrender.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ CGlyph *CGlyphMap::GetGlyph(int Chr, int FontSizeIndex, bool Render)
467467
// couldn't find glyph, render a new one
468468
if(r.empty())
469469
{
470-
Index.m_pGlyph = (CGlyph *) m_Heap.Allocate(sizeof(CGlyph));
470+
Index.m_pGlyph = (CGlyph *) m_Heap.Allocate(sizeof(CGlyph), alignof(CGlyph));
471471
Index.m_pGlyph->m_Rendered = false;
472472
Index.m_pGlyph->m_ID = Chr;
473473
Index.m_pGlyph->m_FontSizeIndex = FontSizeIndex;

src/engine/shared/console.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ void CConsole::RegisterTemp(const char *pName, const char *pParams, int Flags, c
982982
}
983983
else
984984
{
985-
pCommand = new(m_TempCommands.Allocate(sizeof(CCommand))) CCommand(false);
985+
pCommand = new(m_TempCommands.Allocate(sizeof(CCommand), alignof(CCommand))) CCommand(false);
986986
char *pMem = static_cast<char *>(m_TempCommands.Allocate(TEMPCMD_NAME_LENGTH));
987987
str_copy(pMem, pName, TEMPCMD_NAME_LENGTH);
988988
pCommand->m_pName = pMem;

src/engine/shared/console.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class CConsole : public IConsole
149149

150150
void AddEntry()
151151
{
152-
CQueueEntry *pEntry = static_cast<CQueueEntry *>(m_Queue.Allocate(sizeof(CQueueEntry)));
152+
CQueueEntry *pEntry = static_cast<CQueueEntry *>(m_Queue.Allocate(sizeof(CQueueEntry), alignof(CQueueEntry)));
153153
pEntry->m_pNext = 0;
154154
if(!m_pFirst)
155155
m_pFirst = pEntry;

src/engine/shared/demo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ void CDemoPlayer::ScanFile()
455455
if(ChunkType & CHUNKTICKFLAG_KEYFRAME)
456456
{
457457
// save the position
458-
CKeyFrameSearch *pKey = (CKeyFrameSearch *) Heap.Allocate(sizeof(CKeyFrameSearch));
458+
CKeyFrameSearch *pKey = (CKeyFrameSearch *) Heap.Allocate(sizeof(CKeyFrameSearch), alignof(CKeyFrameSearch));
459459
pKey->m_Frame.m_Filepos = CurrentPos;
460460
pKey->m_Frame.m_Tick = ChunkTick;
461461
pKey->m_pNext = 0;

src/engine/shared/mapchecker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void CMapChecker::AddMaplist(const json_value *pMapList)
5151
if(sha256_from_str(&Sha256, pSha256) == -1)
5252
continue;
5353

54-
CWhitelistEntry *pEntry = (CWhitelistEntry *) m_Whitelist.Allocate(sizeof(CWhitelistEntry));
54+
CWhitelistEntry *pEntry = (CWhitelistEntry *) m_Whitelist.Allocate(sizeof(CWhitelistEntry), alignof(CWhitelistEntry));
5555
pEntry->m_pNext = m_pFirst;
5656
m_pFirst = pEntry;
5757

@@ -82,7 +82,7 @@ void CMapChecker::AddMaplist(const CMapVersion *pMaplist, unsigned Num)
8282

8383
for(unsigned i = 0; i < Num; ++i)
8484
{
85-
CWhitelistEntry *pEntry = (CWhitelistEntry *) m_Whitelist.Allocate(sizeof(CWhitelistEntry));
85+
CWhitelistEntry *pEntry = (CWhitelistEntry *) m_Whitelist.Allocate(sizeof(CWhitelistEntry), alignof(CWhitelistEntry));
8686
pEntry->m_pNext = m_pFirst;
8787
m_pFirst = pEntry;
8888

src/engine/shared/memheap.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ void CHeap::NewChunk()
2323
}
2424

2525
//****************
26-
void *CHeap::AllocateFromChunk(unsigned int Size)
26+
void *CHeap::AllocateFromChunk(unsigned int Size, unsigned int Alignment)
2727
{
28+
unsigned int Offset = ((unsigned long) m_pCurrent->m_pCurrent) % Alignment;
29+
if(Offset)
30+
Offset = Alignment - Offset;
31+
2832
// check if we need can fit the allocation
29-
if(m_pCurrent->m_pCurrent + Size > m_pCurrent->m_pEnd)
33+
if(m_pCurrent->m_pCurrent + Size + Offset > m_pCurrent->m_pEnd)
3034
return (void *) 0x0;
3135

3236
// get memory and move the pointer forward
33-
char *pMem = m_pCurrent->m_pCurrent;
34-
m_pCurrent->m_pCurrent += Size;
37+
char *pMem = m_pCurrent->m_pCurrent + Offset;
38+
m_pCurrent->m_pCurrent += Size + Offset;
3539
return pMem;
3640
}
3741

@@ -69,17 +73,17 @@ void CHeap::Clear()
6973
}
7074

7175
//
72-
void *CHeap::Allocate(unsigned int Size)
76+
void *CHeap::Allocate(unsigned int Size, unsigned int Alignment)
7377
{
7478
// try to allocate from current chunk
75-
char *pMem = (char *) AllocateFromChunk(Size);
79+
char *pMem = (char *) AllocateFromChunk(Size, Alignment);
7680
if(!pMem)
7781
{
7882
// allocate new chunk and add it to the heap
7983
NewChunk();
8084

8185
// try to allocate again
82-
pMem = (char *) AllocateFromChunk(Size);
86+
pMem = (char *) AllocateFromChunk(Size, Alignment);
8387
}
8488

8589
return pMem;
@@ -88,7 +92,7 @@ void *CHeap::Allocate(unsigned int Size)
8892
const char *CHeap::StoreString(const char *pSrc)
8993
{
9094
const int Size = str_length(pSrc) + 1;
91-
char *pMem = static_cast<char *>(Allocate(Size));
95+
char *pMem = static_cast<char *>(Allocate(Size, alignof(char)));
9296
str_copy(pMem, pSrc, Size);
9397
return pMem;
9498
}

0 commit comments

Comments
 (0)