diff --git a/src/engine/client/backend_sdl.cpp b/src/engine/client/backend_sdl.cpp index 8bfd2ae2e..1dc434a64 100644 --- a/src/engine/client/backend_sdl.cpp +++ b/src/engine/client/backend_sdl.cpp @@ -326,7 +326,9 @@ void *CCommandProcessorFragment_OpenGL::Resize(int Width, int Height, int NewWid int Bpp = TexFormatToImageColorChannelCount(Format); - pTmpData = (unsigned char *)malloc((size_t)NewWidth * NewHeight * Bpp); + // All calls to Resize() ensure width & height are > 0, Bpp is always > 0, + // thus no allocation size 0 possible. + pTmpData = (unsigned char *)malloc((size_t)NewWidth * NewHeight * Bpp); // NOLINT(clang-analyzer-optin.portability.UnixAPI) ResizeImage((uint8_t *)pData, Width, Height, (uint8_t *)pTmpData, NewWidth, NewHeight, Bpp); diff --git a/src/engine/shared/demo.cpp b/src/engine/shared/demo.cpp index dd0ef103c..ce629e06d 100644 --- a/src/engine/shared/demo.cpp +++ b/src/engine/shared/demo.cpp @@ -531,9 +531,12 @@ void CDemoPlayer::ScanFile() } // copy all the frames to an array instead for fast access - m_pKeyFrames = (CKeyFrame *)calloc(m_Info.m_SeekablePoints, sizeof(CKeyFrame)); - for(pCurrentKey = pFirstKey, i = 0; pCurrentKey; pCurrentKey = pCurrentKey->m_pNext, i++) - m_pKeyFrames[i] = pCurrentKey->m_Frame; + if(m_Info.m_SeekablePoints > 0) + { + m_pKeyFrames = (CKeyFrame *)calloc(m_Info.m_SeekablePoints, sizeof(CKeyFrame)); + for(pCurrentKey = pFirstKey, i = 0; pCurrentKey; pCurrentKey = pCurrentKey->m_pNext, i++) + m_pKeyFrames[i] = pCurrentKey->m_Frame; + } // destroy the temporary heap and seek back to the start io_seek(m_File, StartPos, IOSEEK_START); @@ -1040,8 +1043,11 @@ int CDemoPlayer::Stop() m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "demo_player", "Stopped playback"); io_close(m_File); m_File = 0; - free(m_pKeyFrames); - m_pKeyFrames = 0; + if(m_pKeyFrames) + { + free(m_pKeyFrames); + m_pKeyFrames = 0; + } str_copy(m_aFilename, "", sizeof(m_aFilename)); return 0; } diff --git a/src/game/client/components/mapimages.cpp b/src/game/client/components/mapimages.cpp index 9718f564f..2336f3a78 100644 --- a/src/game/client/components/mapimages.cpp +++ b/src/game/client/components/mapimages.cpp @@ -217,7 +217,7 @@ IGraphics::CTextureHandle CMapImages::GetEntities(EMapImageEntityLayerType Entit if(ImagePNGLoaded && ImgInfo.m_Width > 0 && ImgInfo.m_Height > 0) { - int ColorChannelCount = 0; + int ColorChannelCount = 4; if(ImgInfo.m_Format == CImageInfo::FORMAT_ALPHA) ColorChannelCount = 1; else if(ImgInfo.m_Format == CImageInfo::FORMAT_RGB) diff --git a/src/game/editor/io.cpp b/src/game/editor/io.cpp index 12acd8d83..4a49f4a65 100644 --- a/src/game/editor/io.cpp +++ b/src/game/editor/io.cpp @@ -270,7 +270,8 @@ int CEditorMap::Save(class IStorage *pStorage, const char *pFileName) Size += str_length(m_lSettings[i].m_aCommand) + 1; } - char *pSettings = (char *)malloc(Size); + // Checked that m_lSettings.size() is not 0, thus Size is > 0 as ell + char *pSettings = (char *)malloc(Size); // NOLINT(clang-analyzer-optin.portability.UnixAPI) char *pNext = pSettings; for(int i = 0; i < m_lSettings.size(); i++) { @@ -542,20 +543,23 @@ int CEditorMap::Save(class IStorage *pStorage, const char *pFileName) PointCount += Item.m_NumPoints; } - // save points - int TotalSize = sizeof(CEnvPoint) * PointCount; - CEnvPoint *pPoints = (CEnvPoint *)calloc(PointCount, sizeof(*pPoints)); - PointCount = 0; - - for(int e = 0; e < m_lEnvelopes.size(); e++) + if(PointCount > 0) { - int Count = m_lEnvelopes[e]->m_lPoints.size(); - mem_copy(&pPoints[PointCount], m_lEnvelopes[e]->m_lPoints.base_ptr(), sizeof(CEnvPoint) * Count); - PointCount += Count; - } + // save points + int TotalSize = sizeof(CEnvPoint) * PointCount; + CEnvPoint *pPoints = (CEnvPoint *)calloc(PointCount, sizeof(*pPoints)); + PointCount = 0; - df.AddItem(MAPITEMTYPE_ENVPOINTS, 0, TotalSize, pPoints); - free(pPoints); + for(int e = 0; e < m_lEnvelopes.size(); e++) + { + int Count = m_lEnvelopes[e]->m_lPoints.size(); + mem_copy(&pPoints[PointCount], m_lEnvelopes[e]->m_lPoints.base_ptr(), sizeof(CEnvPoint) * Count); + PointCount += Count; + } + + df.AddItem(MAPITEMTYPE_ENVPOINTS, 0, TotalSize, pPoints); + free(pPoints); + } // finish the data file df.Finish(); diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 8f55c0953..06b796954 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -3335,7 +3335,7 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) } io_close(File); - char *pSettings = (char *)malloc(TotalLength); + char *pSettings = (char *)malloc(maximum(1, TotalLength)); int Offset = 0; for(int i = 0; i < aLines.size(); i++) { @@ -3374,6 +3374,7 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) if(DataSize == TotalLength && mem_comp(pSettings, pMapSettings, DataSize) == 0) { // Configs coincide, no need to update map. + free(pSettings); return; } Reader.UnloadData(pInfo->m_Settings); @@ -3423,6 +3424,7 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) } dbg_msg("mapchange", "imported settings"); + free(pSettings); Reader.Close(); Writer.OpenFile(Storage(), aTemp); Writer.Finish(); diff --git a/src/tools/config_store.cpp b/src/tools/config_store.cpp index cf3659406..831ca0f98 100644 --- a/src/tools/config_store.cpp +++ b/src/tools/config_store.cpp @@ -31,7 +31,7 @@ void Process(IStorage *pStorage, const char *pMapName, const char *pConfigName) } io_close(File); - pSettings = (char *)malloc(TotalLength); + pSettings = (char *)malloc(maximum(1, TotalLength)); int Offset = 0; for(int i = 0; i < aLines.size(); i++) { @@ -73,6 +73,7 @@ void Process(IStorage *pStorage, const char *pMapName, const char *pConfigName) if(DataSize == TotalLength && mem_comp(pSettings, pMapSettings, DataSize) == 0) { dbg_msg("config_store", "configs coincide, not updating map"); + free(pSettings); return; } Reader.UnloadData(pInfo->m_Settings); @@ -121,6 +122,7 @@ void Process(IStorage *pStorage, const char *pMapName, const char *pConfigName) Reader.UnloadData(i); } + free(pSettings); Reader.Close(); if(!Writer.OpenFile(pStorage, pMapName)) {