From c5906c4bb39283d7a34c9eb13dbfe22d4283b356 Mon Sep 17 00:00:00 2001 From: xezon <4720891+xezon@users.noreply.github.com> Date: Fri, 6 Sep 2024 10:10:03 +0200 Subject: [PATCH] Refactor magic numbers --- .../client/gui/gamewindowmanagerscript.cpp | 4 +-- src/game/client/maputil.cpp | 4 +-- src/game/logic/ai/aigroup.cpp | 16 ++++++------ src/platform/audio/milesaudiofilecache.cpp | 1 + src/platform/w3dengine/client/heightmap.cpp | 2 +- .../w3dengine/client/worldheightmap.cpp | 4 +-- src/w3d/lib/ffactory.cpp | 2 +- src/w3d/renderer/assetmgr.cpp | 19 +++++++------- src/w3d/renderer/dx8renderer.cpp | 26 +++++++++---------- src/w3d/renderer/dx8renderer.h | 3 +++ src/w3d/renderer/hlod.cpp | 20 +++++++------- src/w3d/renderer/meshgeometry.cpp | 2 +- src/w3d/renderer/meshmdl.cpp | 2 +- src/w3d/renderer/motchan.cpp | 4 +-- src/w3d/renderer/textureloader.cpp | 2 +- src/w3d/renderer/w3d.cpp | 4 +-- src/w3d/renderer/w3dformat.cpp | 4 +-- 17 files changed, 61 insertions(+), 58 deletions(-) diff --git a/src/game/client/gui/gamewindowmanagerscript.cpp b/src/game/client/gui/gamewindowmanagerscript.cpp index ef4f4e7e3..b17b78cbf 100644 --- a/src/game/client/gui/gamewindowmanagerscript.cpp +++ b/src/game/client/gui/gamewindowmanagerscript.cpp @@ -1583,11 +1583,11 @@ GameWindow *Parse_Window(File *in_file, char *buffer) GameWindow *GameWindowManager::Win_Create_From_Script(Utf8String filename, WindowLayoutInfo *info) { static char buffer[2096]; - char path[260]; + char path[PATH_MAX]; const char *fname = filename.Str(); GameWindow *window = nullptr; + memset(path, 0, sizeof(path)); strcpy(path, "Window\\"); - memset(&path[8], 0, 0xFCu); WindowLayoutInfo new_info; Utf8String str; Reset_Window_Stack(); diff --git a/src/game/client/maputil.cpp b/src/game/client/maputil.cpp index a393cc118..90bbefd4e 100644 --- a/src/game/client/maputil.cpp +++ b/src/game/client/maputil.cpp @@ -461,7 +461,7 @@ bool Load_Map(Utf8String name) char name_without_ext[PATH_MAX]; if (len >= 4) { - memset(name_without_ext, 0, PATH_MAX); + memset(name_without_ext, 0, sizeof(name_without_ext)); strncpy(name_without_ext, name_copy, len - 4); } @@ -499,7 +499,7 @@ unsigned int Calc_CRC(Utf8String dir, Utf8String name) char name_without_ext[PATH_MAX]; if (len >= 4) { - memset(name_without_ext, 0, PATH_MAX); + memset(name_without_ext, 0, sizeof(name_without_ext)); strncpy(name_without_ext, name_copy, len - 4); } diff --git a/src/game/logic/ai/aigroup.cpp b/src/game/logic/ai/aigroup.cpp index f5fd5ea06..a272b97eb 100644 --- a/src/game/logic/ai/aigroup.cpp +++ b/src/game/logic/ai/aigroup.cpp @@ -718,13 +718,13 @@ bool AIGroup::Friend_Move_Infantry_To_Pos(const Coord3D *pos, CommandSourceType int i7 = 10000; int i8 = 10000; - for (int j = 0; j < 3; j++) { + for (int j = 0; j < ARRAY_SIZE(counts1); j++) { if (counts1[j] < i7) { i7 = counts1[j]; } } - for (int j = 0; j < 5; j++) { + for (int j = 0; j < ARRAY_SIZE(counts2); j++) { if (counts2[j] < i8) { i8 = counts2[j]; } @@ -733,7 +733,7 @@ bool AIGroup::Friend_Move_Infantry_To_Pos(const Coord3D *pos, CommandSourceType int i9 = 10000; int i10 = -1; - for (int j = 0; j < 3; j++) { + for (int j = 0; j < ARRAY_SIZE(counts1); j++) { if (counts1[j] == i7) { int i11 = i6 + 1 - j; @@ -756,7 +756,7 @@ bool AIGroup::Friend_Move_Infantry_To_Pos(const Coord3D *pos, CommandSourceType i9 = 10000; i10 = -1; - for (int j = 0; j < 5; j++) { + for (int j = 0; j < ARRAY_SIZE(counts2); j++) { if (counts2[j] == i8) { int i12 = i5 + 2 - j; @@ -1204,12 +1204,12 @@ bool AIGroup::Friend_Move_Vehicle_To_Pos(const Coord3D *pos, CommandSourceType c int i7 = 10000; int i8 = 10000; - for (int j = 0; j < 3; j += 2) { + for (int j = 0; j < ARRAY_SIZE(counts1); j += 2) { if (counts1[j] < i7) { i7 = counts1[j]; } } - for (int j = 0; j < 3; j++) { + for (int j = 0; j < ARRAY_SIZE(counts2); j++) { if (counts2[j] < i8) { i8 = counts2[j]; } @@ -1218,7 +1218,7 @@ bool AIGroup::Friend_Move_Vehicle_To_Pos(const Coord3D *pos, CommandSourceType c int i9 = 10000; int i10 = -1; - for (int j = 0; j < 3; j += 2) { + for (int j = 0; j < ARRAY_SIZE(counts1); j += 2) { if (counts1[j] == i7) { int i11 = i6 + 1 - j; @@ -1241,7 +1241,7 @@ bool AIGroup::Friend_Move_Vehicle_To_Pos(const Coord3D *pos, CommandSourceType c i9 = 10000; i10 = -1; - for (int j = 0; j < 3; j++) { + for (int j = 0; j < ARRAY_SIZE(counts2); j++) { if (counts2[j] == i8) { int i12 = i5 + 1 - j; diff --git a/src/platform/audio/milesaudiofilecache.cpp b/src/platform/audio/milesaudiofilecache.cpp index d70f5a4c1..5e999518e 100644 --- a/src/platform/audio/milesaudiofilecache.cpp +++ b/src/platform/audio/milesaudiofilecache.cpp @@ -115,6 +115,7 @@ AudioDataHandle MilesAudioFileCache::Open_File(const AudioEventRTS *audio_event) open_audio.wave_data = file_data; } + static_assert(sizeof(open_audio.info) == sizeof(sound_info), "Expected equal"); memcpy(&open_audio.info, &sound_info, sizeof(sound_info)); open_audio.data_size = file_size; open_audio.ref_count = 1; diff --git a/src/platform/w3dengine/client/heightmap.cpp b/src/platform/w3dengine/client/heightmap.cpp index 863a971db..2d9f8964d 100644 --- a/src/platform/w3dengine/client/heightmap.cpp +++ b/src/platform/w3dengine/client/heightmap.cpp @@ -479,7 +479,7 @@ int HeightMapRenderObjClass::Init_Height_Data( if (map->Get_Extra_Alpha_UV_Data(x_pos, y_pos, u, v, alpha, &need_flip, &is_cliff)) { if (m_numExtraBlendTiles >= m_extraBlendTilePositionsSize) { unsigned int *tiles = new unsigned int[m_extraBlendTilePositionsSize + 512]; - memcpy(tiles, m_extraBlendTilePositions, 4 * m_extraBlendTilePositionsSize); + memcpy(tiles, m_extraBlendTilePositions, sizeof(unsigned int) * m_extraBlendTilePositionsSize); delete[] m_extraBlendTilePositions; m_extraBlendTilePositions = tiles; m_extraBlendTilePositionsSize += 512; diff --git a/src/platform/w3dengine/client/worldheightmap.cpp b/src/platform/w3dengine/client/worldheightmap.cpp index 398f3d072..d5b3c8718 100644 --- a/src/platform/w3dengine/client/worldheightmap.cpp +++ b/src/platform/w3dengine/client/worldheightmap.cpp @@ -646,7 +646,7 @@ bool WorldHeightMap::Parse_Blend_Tile_Data(DataChunkInput &file, DataChunkInfo * file.Read_Byte_Array(reinterpret_cast(m_extraBlendTileNdxes), 2 * m_dataSize); if (!g_theWriteableGlobalData->m_use3WayTerrainBlends) { - memset(m_extraBlendTileNdxes, 0, 2 * m_dataSize); + memset(m_extraBlendTileNdxes, 0, sizeof(short) * m_dataSize); } } @@ -2033,7 +2033,7 @@ void WorldHeightMap::Setup_Alpha_Tiles() if (!s_alphaTiles[0]) { for (int i = 0; i < 12; i++) { bool b[16]; - memset(b, false, 16); + memset(b, false, sizeof(b)); int i2 = i; if (i >= 6) { diff --git a/src/w3d/lib/ffactory.cpp b/src/w3d/lib/ffactory.cpp index 36e35e55c..467d6dcbf 100644 --- a/src/w3d/lib/ffactory.cpp +++ b/src/w3d/lib/ffactory.cpp @@ -91,7 +91,7 @@ FileClass *SimpleFileFactoryClass::Get_File(const char *filename) new_name.Format("%s%s", token, stripped_name.Peek_Buffer()); file->Set_Name(new_name); - if (file->Open(1)) { + if (file->Open(FM_READ)) { file->Close(); break; } diff --git a/src/w3d/renderer/assetmgr.cpp b/src/w3d/renderer/assetmgr.cpp index 82798e567..f1304ffb5 100644 --- a/src/w3d/renderer/assetmgr.cpp +++ b/src/w3d/renderer/assetmgr.cpp @@ -120,7 +120,7 @@ W3DAssetManager::W3DAssetManager() : Register_Prototype_Loader(&s_sphereLoader); Register_Prototype_Loader(&s_particleEmitterLoader); m_prototypeHashTable = new PrototypeClass *[PROTOTYPE_HASH_TABLE_SIZE]; - memset(m_prototypeHashTable, 0, PROTOTYPE_HASH_TABLE_SIZE * sizeof(uintptr_t)); + memset(m_prototypeHashTable, 0, PROTOTYPE_HASH_TABLE_SIZE * sizeof(PrototypeClass *)); } // 0x008143C0 @@ -191,7 +191,7 @@ void W3DAssetManager::Free_Assets() } } - memset(m_prototypeHashTable, 0, PROTOTYPE_HASH_TABLE_SIZE * sizeof(uintptr_t)); + memset(m_prototypeHashTable, 0, PROTOTYPE_HASH_TABLE_SIZE * sizeof(PrototypeClass *)); m_hAnimManager.Free_All_Anims(); m_hTreeManager.Free_All_Trees(); Release_All_Textures(); @@ -219,7 +219,7 @@ void W3DAssetManager::Free_Assets_With_Exclusion_List(DynamicVectorClass texture(m_textureHash); !texture.Is_Done(); texture.Next()) { @@ -421,7 +420,7 @@ void W3DAssetManager::Release_Unused_Textures() unused_textures[unused_textures_count++] = texture.Peek_Value(); - if (unused_textures_count < unused_textures_size) { + if (unused_textures_count < ARRAY_SIZE(unused_textures)) { continue; } @@ -508,7 +507,7 @@ HTreeClass *W3DAssetManager::Get_HTree(const char *name) } char asset_filename[256]{}; - snprintf(asset_filename, 256, "%s.w3d", name); + snprintf(asset_filename, ARRAY_SIZE(asset_filename), "%s.w3d", name); if (Load_3D_Assets(asset_filename) == false) { StringClass new_filename = StringClass{ "..\\", true } + asset_filename; diff --git a/src/w3d/renderer/dx8renderer.cpp b/src/w3d/renderer/dx8renderer.cpp index b72ab7899..b77f40f71 100644 --- a/src/w3d/renderer/dx8renderer.cpp +++ b/src/w3d/renderer/dx8renderer.cpp @@ -81,7 +81,7 @@ bool Textures_Material_And_Shader_Booking_Struct::Add_Textures_Material_And_Shad for (unsigned int i = 0; i < m_addedTypeCount; i++) { bool b = true; - for (int j = 0; j < 2; j++) { + for (int j = 0; j < ARRAY_SIZE(m_addedTextures); j++) { b = b && texs[j] == m_addedTextures[j][i]; } @@ -91,7 +91,7 @@ bool Textures_Material_And_Shader_Booking_Struct::Add_Textures_Material_And_Shad } captainslog_assert(m_addedTypeCount < MAX_ADDED_TYPE_COUNT); - for (int k = 0; k < 2; k++) { + for (int k = 0; k < ARRAY_SIZE(m_addedTextures); k++) { m_addedTextures[k][m_addedTypeCount] = texs[k]; } @@ -207,7 +207,7 @@ TextureClass *Vertex_Split_Table::Peek_Texture(unsigned int pidx, unsigned int p if (pidx < (unsigned int)m_mmc->Get_Polygon_Count()) { return m_mmc->Peek_Texture(pidx, pass, stage); } else { - captainslog_dbgassert(0, "GapFillerClass removed"); + captainslog_dbgassert(false, "GapFillerClass removed"); return nullptr; } } else { @@ -221,7 +221,7 @@ VertexMaterialClass *Vertex_Split_Table::Peek_Material(unsigned int pidx, unsign if (pidx < (unsigned int)m_mmc->Get_Polygon_Count()) { return m_mmc->Peek_Material(m_mmc->Get_Polygon_Array()[pidx][0], pass); } else { - captainslog_dbgassert(0, "GapFillerClass removed"); + captainslog_dbgassert(false, "GapFillerClass removed"); return nullptr; } } else { @@ -235,7 +235,7 @@ ShaderClass Vertex_Split_Table::Peek_Shader(unsigned int pidx, unsigned int pass if (pidx < (unsigned int)m_mmc->Get_Polygon_Count()) { return m_mmc->Get_Shader(pidx, pass); } else { - captainslog_dbgassert(0, "GapFillerClass removed"); + captainslog_dbgassert(false, "GapFillerClass removed"); return ShaderClass(); } } else { @@ -471,7 +471,7 @@ DX8TextureCategoryClass::DX8TextureCategoryClass( captainslog_assert(pass >= 0); captainslog_assert(pass < DX8FVFCategoryContainer::MAX_PASSES); - for (int i = 0; i < 2; i++) { + for (int i = 0; i < ARRAY_SIZE(m_textures); i++) { m_textures[i] = nullptr; Ref_Ptr_Set(m_textures[i], texs[i]); } @@ -493,7 +493,7 @@ DX8TextureCategoryClass::~DX8TextureCategoryClass() g_theDX8MeshRenderer.Unregister_Mesh_Type(r->Get_Mesh_Model_Class()); } - for (int i = 0; i < 2; i++) { + for (int i = 0; i < ARRAY_SIZE(m_textures); i++) { Ref_Ptr_Release(m_textures[i]); } @@ -510,7 +510,7 @@ void DX8TextureCategoryClass::Add_Render_Task(DX8PolygonRendererClass *p_rendere void DX8TextureCategoryClass::Render() { - for (int i = 0; i < 2; i++) { + for (int i = 0; i < ARRAY_SIZE(m_textures); i++) { DX8Wrapper::Set_Texture(i, Peek_Texture(i)); } @@ -931,7 +931,7 @@ void DX8FVFCategoryContainer::Change_Polygon_Renderer_Texture(MultiListClassPeek_Texture(0); tmp_textures[1] = texcat->Peek_Texture(1); tmp_textures[stage] = new_texture; @@ -1012,7 +1012,7 @@ void DX8FVFCategoryContainer::Change_Polygon_Renderer_Material( DX8TextureCategoryClass *tc = Find_Matching_Texture_Category(new_vmat, pass, texcat); if (tc == nullptr) { - TextureClass *tmp_textures[2]; + TextureClass *tmp_textures[DX8TextureCategoryClass::TextureSize]; tmp_textures[0] = texcat->Peek_Texture(0); tmp_textures[1] = texcat->Peek_Texture(1); tc = new DX8TextureCategoryClass(this, tmp_textures, texcat->Get_Shader(), new_vmat, pass); @@ -1132,7 +1132,7 @@ DX8TextureCategoryClass *DX8FVFCategoryContainer::Find_Matching_Texture_Category DX8TextureCategoryClass *tc = dest_it.Peek_Obj(); bool b = true; - for (int i = 0; i < 2; i++) { + for (int i = 0; i < DX8TextureCategoryClass::TextureSize; i++) { b = b & (tc->Peek_Texture(i) == ref_category->Peek_Texture(i)); } @@ -1159,7 +1159,7 @@ DX8TextureCategoryClass *DX8FVFCategoryContainer::Find_Matching_Texture_Category DX8TextureCategoryClass *tc = dest_it.Peek_Obj(); bool b = true; - for (int i = 0; i < 2; i++) { + for (int i = 0; i < DX8TextureCategoryClass::TextureSize; i++) { if (stage != i) { b = b & (tc->Peek_Texture(i) == ref_category->Peek_Texture(i)); } @@ -1253,7 +1253,7 @@ void DX8FVFCategoryContainer::Generate_Texture_Categories(Vertex_Split_Table &sp int old_used_indices = m_usedIndices; for (int j = 0; j < polygon_count; j++) { - TextureClass *texs[2]; + TextureClass *texs[DX8TextureCategoryClass::TextureSize]; texs[0] = split_table.Peek_Texture(j, i, 0); texs[1] = split_table.Peek_Texture(j, i, 1); VertexMaterialClass *vmat = split_table.Peek_Material(j, i); diff --git a/src/w3d/renderer/dx8renderer.h b/src/w3d/renderer/dx8renderer.h index d5df59622..c5a3ce4a2 100644 --- a/src/w3d/renderer/dx8renderer.h +++ b/src/w3d/renderer/dx8renderer.h @@ -122,6 +122,9 @@ class DX8TextureCategoryClass : public MultiListObjectClass #else static bool s_forceMultiply; #endif + +public: + static constexpr int TextureSize = ARRAY_SIZE(m_textures); // Thyme specific }; class DX8FVFCategoryContainer : public MultiListObjectClass diff --git a/src/w3d/renderer/hlod.cpp b/src/w3d/renderer/hlod.cpp index 6c059390b..686049edf 100644 --- a/src/w3d/renderer/hlod.cpp +++ b/src/w3d/renderer/hlod.cpp @@ -168,8 +168,8 @@ bool HLodDefClass::SubObjectArrayClass::Save_W3D(ChunkSaveClass &csave) W3dHLodSubObjectStruct subobjheader; memset(&subobjheader, 0, sizeof(subobjheader)); subobjheader.BoneIndex = m_boneIndex[i]; - strncpy(subobjheader.Name, m_modelName[i], 32); - subobjheader.Name[31] = 0; + strncpy(subobjheader.Name, m_modelName[i], ARRAY_SIZE(subobjheader.Name)); + subobjheader.Name[ARRAY_SIZE(subobjheader.Name) - 1] = 0; b = csave.Write(&subobjheader, sizeof(W3dHLodSubObjectStruct)) == sizeof(W3dHLodSubObjectStruct); csave.End_Chunk(); } @@ -336,10 +336,10 @@ W3DErrorType HLodDefClass::Save_Header(ChunkSaveClass &csave) memset(&header, 0, sizeof(header)); header.Version = 0x10000; header.LodCount = m_lodCount; - strncpy(header.Name, m_name, 16); - header.Name[15] = 0; - strncpy(header.HierarchyName, m_hierarchyTreeName, 16); - header.HierarchyName[15] = 0; + strncpy(header.Name, m_name, ARRAY_SIZE(header.Name)); + header.Name[ARRAY_SIZE(header.Name) - 1] = 0; + strncpy(header.HierarchyName, m_hierarchyTreeName, ARRAY_SIZE(header.HierarchyName)); + header.HierarchyName[ARRAY_SIZE(header.HierarchyName) - 1] = 0; W3DErrorType error = W3D_ERROR_OK; @@ -1226,8 +1226,8 @@ void HLodClass::Include_NULL_Lod(bool include) float *costs = new float[m_lodCount]; float *values = new float[m_lodCount + 1]; - memcpy(costs, &m_cost + 1, m_lodCount * 4); - memcpy(values, &m_value + 1, (m_lodCount * 4) + 4); + memcpy(costs, &m_cost + 1, m_lodCount * sizeof(float)); + memcpy(values, &m_value + 1, (m_lodCount * sizeof(float)) + sizeof(float)); delete[] m_lod; delete[] m_cost; @@ -1253,8 +1253,8 @@ void HLodClass::Include_NULL_Lod(bool include) float *costs = new float[m_lodCount]; float *values = new float[m_lodCount + 1]; - memcpy(costs + 1, &m_cost, m_lodCount * 4); - memcpy(values + 1, &m_value, (m_lodCount * 4) + 4); + memcpy(costs + 1, &m_cost, m_lodCount * sizeof(float)); + memcpy(values + 1, &m_value, (m_lodCount * sizeof(float)) + sizeof(float)); delete[] m_lod; delete[] m_cost; diff --git a/src/w3d/renderer/meshgeometry.cpp b/src/w3d/renderer/meshgeometry.cpp index e9b41f290..f235ef734 100644 --- a/src/w3d/renderer/meshgeometry.cpp +++ b/src/w3d/renderer/meshgeometry.cpp @@ -875,7 +875,7 @@ W3DErrorType MeshGeometryClass::Load_W3D(ChunkLoadClass &cload) m_w3dAttributes = header.Attributes; m_sortLevel = header.SortLevel; tmpname = new char[namelen]; - memset(tmpname, 0, namelen); + memset(tmpname, 0, sizeof(char) * namelen); if (strlen(header.ContainerName) > 0) { strcpy(tmpname, header.ContainerName); diff --git a/src/w3d/renderer/meshmdl.cpp b/src/w3d/renderer/meshmdl.cpp index 5313f7f49..15e43723a 100644 --- a/src/w3d/renderer/meshmdl.cpp +++ b/src/w3d/renderer/meshmdl.cpp @@ -365,7 +365,7 @@ W3DErrorType MeshModelClass::Load_W3D(ChunkLoadClass &cload) m_w3dAttributes = context->m_header.Attributes; m_sortLevel = context->m_header.SortLevel; tmpname = new char[namelen]; - memset(tmpname, 0, namelen); + memset(tmpname, 0, sizeof(char) * namelen); if (strlen(context->m_header.ContainerName) > 0) { strcpy(tmpname, context->m_header.ContainerName); diff --git a/src/w3d/renderer/motchan.cpp b/src/w3d/renderer/motchan.cpp index dfe7a21cf..5f8f4db79 100644 --- a/src/w3d/renderer/motchan.cpp +++ b/src/w3d/renderer/motchan.cpp @@ -545,14 +545,14 @@ float AdaptiveDeltaMotionChannelClass::Get_Frame(unsigned int frame_idx, unsigne m_cacheFrame = frame_idx; return m_cacheData[vector_idx]; } else if (frame_idx == m_cacheFrame + 2) { - memcpy(m_cacheData, &m_cacheData[m_vectorLen], 4 * m_vectorLen); + memcpy(m_cacheData, &m_cacheData[m_vectorLen], sizeof(float) * m_vectorLen); Decompress(++m_cacheFrame, m_cacheData, frame_idx, &m_cacheData[m_vectorLen]); return m_cacheData[vector_idx + m_vectorLen]; } else { captainslog_assert(m_vectorLen < 4); - memcpy(dst, &m_cacheData[m_vectorLen], 4 * m_vectorLen); + memcpy(dst, &m_cacheData[m_vectorLen], sizeof(float) * m_vectorLen); Decompress(m_cacheFrame, dst, frame_idx, m_cacheData); m_cacheFrame = frame_idx; diff --git a/src/w3d/renderer/textureloader.cpp b/src/w3d/renderer/textureloader.cpp index 3e199981f..4719803ed 100644 --- a/src/w3d/renderer/textureloader.cpp +++ b/src/w3d/renderer/textureloader.cpp @@ -206,7 +206,7 @@ w3dsurface_t TextureLoader::Load_Surface_Immediate(const StringClass &texture, W } TargaImage targa; - targa.Open(texture, 0); + targa.Open(texture, TargaImage::TARGA_READ); targa.Toggle_Flip_Y(); WW3DFormat dest_format; WW3DFormat src_format; diff --git a/src/w3d/renderer/w3d.cpp b/src/w3d/renderer/w3d.cpp index d4ac34d49..21f15fe8f 100644 --- a/src/w3d/renderer/w3d.cpp +++ b/src/w3d/renderer/w3d.cpp @@ -75,7 +75,7 @@ class StaticSortListClass class DefaultStaticSortListClass : public StaticSortListClass { public: - DefaultStaticSortListClass() : m_minLevel(1), m_maxLevel(32) {} + DefaultStaticSortListClass() : m_minLevel(1), m_maxLevel(ARRAY_SIZE(m_lists) - 1) {} virtual ~DefaultStaticSortListClass() override {} virtual void Add_To_List(RenderObjClass *robj, unsigned int sort_level) override; virtual void Render_And_Clear(RenderInfoClass &rinfo) override; @@ -88,7 +88,7 @@ class DefaultStaticSortListClass : public StaticSortListClass void DefaultStaticSortListClass::Add_To_List(RenderObjClass *robj, unsigned int sort_level) { - if (sort_level >= 1 && sort_level <= 32) { + if (sort_level >= 1 && sort_level <= ARRAY_SIZE(m_lists) - 1) { m_lists[sort_level].Add_Tail(robj, false); } else { captainslog_assert(0); diff --git a/src/w3d/renderer/w3dformat.cpp b/src/w3d/renderer/w3dformat.cpp index ef5665ec7..9ea1930c9 100644 --- a/src/w3d/renderer/w3dformat.cpp +++ b/src/w3d/renderer/w3dformat.cpp @@ -110,7 +110,7 @@ uint32_t WW3DFormat_To_D3DFormat(WW3DFormat format) WW3DFormat D3DFormat_To_WW3DFormat(uint32_t format) { - char buff[5]; + char buff[sizeof(format) + 1]; switch (format) { case FourCC<'D', 'X', 'T', '1'>::value: @@ -133,7 +133,7 @@ WW3DFormat D3DFormat_To_WW3DFormat(uint32_t format) // Log if the engine tries to load a DDS format we don't currently handle. memcpy(buff, &format, sizeof(format)); - buff[4] = '\0'; + buff[sizeof(format)] = '\0'; captainslog_error("TODO Unhandled format FourCC '%s', implementations welcome.", buff); return WW3D_FORMAT_UNKNOWN;