Fix clang-analyzer-optin.portability.UnixAPI

and mark some false positives

/media/ddnet/src/engine/client/backend_sdl.cpp:329:30: warning: Call to 'malloc' has an allocation size of 0 bytes [clang-analyzer-optin.portability.UnixAPI]
This commit is contained in:
def 2020-10-12 16:07:29 +02:00
parent 469888e156
commit a4f13ed4a8
6 changed files with 38 additions and 22 deletions

View file

@ -326,7 +326,9 @@ void *CCommandProcessorFragment_OpenGL::Resize(int Width, int Height, int NewWid
int Bpp = TexFormatToImageColorChannelCount(Format); 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); ResizeImage((uint8_t *)pData, Width, Height, (uint8_t *)pTmpData, NewWidth, NewHeight, Bpp);

View file

@ -531,9 +531,12 @@ void CDemoPlayer::ScanFile()
} }
// copy all the frames to an array instead for fast access // copy all the frames to an array instead for fast access
m_pKeyFrames = (CKeyFrame *)calloc(m_Info.m_SeekablePoints, sizeof(CKeyFrame)); if(m_Info.m_SeekablePoints > 0)
for(pCurrentKey = pFirstKey, i = 0; pCurrentKey; pCurrentKey = pCurrentKey->m_pNext, i++) {
m_pKeyFrames[i] = pCurrentKey->m_Frame; 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 // destroy the temporary heap and seek back to the start
io_seek(m_File, StartPos, IOSEEK_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"); m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "demo_player", "Stopped playback");
io_close(m_File); io_close(m_File);
m_File = 0; m_File = 0;
free(m_pKeyFrames); if(m_pKeyFrames)
m_pKeyFrames = 0; {
free(m_pKeyFrames);
m_pKeyFrames = 0;
}
str_copy(m_aFilename, "", sizeof(m_aFilename)); str_copy(m_aFilename, "", sizeof(m_aFilename));
return 0; return 0;
} }

View file

@ -217,7 +217,7 @@ IGraphics::CTextureHandle CMapImages::GetEntities(EMapImageEntityLayerType Entit
if(ImagePNGLoaded && ImgInfo.m_Width > 0 && ImgInfo.m_Height > 0) if(ImagePNGLoaded && ImgInfo.m_Width > 0 && ImgInfo.m_Height > 0)
{ {
int ColorChannelCount = 0; int ColorChannelCount = 4;
if(ImgInfo.m_Format == CImageInfo::FORMAT_ALPHA) if(ImgInfo.m_Format == CImageInfo::FORMAT_ALPHA)
ColorChannelCount = 1; ColorChannelCount = 1;
else if(ImgInfo.m_Format == CImageInfo::FORMAT_RGB) else if(ImgInfo.m_Format == CImageInfo::FORMAT_RGB)

View file

@ -270,7 +270,8 @@ int CEditorMap::Save(class IStorage *pStorage, const char *pFileName)
Size += str_length(m_lSettings[i].m_aCommand) + 1; 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; char *pNext = pSettings;
for(int i = 0; i < m_lSettings.size(); i++) 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; PointCount += Item.m_NumPoints;
} }
// save points if(PointCount > 0)
int TotalSize = sizeof(CEnvPoint) * PointCount;
CEnvPoint *pPoints = (CEnvPoint *)calloc(PointCount, sizeof(*pPoints));
PointCount = 0;
for(int e = 0; e < m_lEnvelopes.size(); e++)
{ {
int Count = m_lEnvelopes[e]->m_lPoints.size(); // save points
mem_copy(&pPoints[PointCount], m_lEnvelopes[e]->m_lPoints.base_ptr(), sizeof(CEnvPoint) * Count); int TotalSize = sizeof(CEnvPoint) * PointCount;
PointCount += Count; CEnvPoint *pPoints = (CEnvPoint *)calloc(PointCount, sizeof(*pPoints));
} PointCount = 0;
df.AddItem(MAPITEMTYPE_ENVPOINTS, 0, TotalSize, pPoints); for(int e = 0; e < m_lEnvelopes.size(); e++)
free(pPoints); {
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 // finish the data file
df.Finish(); df.Finish();

View file

@ -3335,7 +3335,7 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize)
} }
io_close(File); io_close(File);
char *pSettings = (char *)malloc(TotalLength); char *pSettings = (char *)malloc(maximum(1, TotalLength));
int Offset = 0; int Offset = 0;
for(int i = 0; i < aLines.size(); i++) 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) if(DataSize == TotalLength && mem_comp(pSettings, pMapSettings, DataSize) == 0)
{ {
// Configs coincide, no need to update map. // Configs coincide, no need to update map.
free(pSettings);
return; return;
} }
Reader.UnloadData(pInfo->m_Settings); Reader.UnloadData(pInfo->m_Settings);
@ -3423,6 +3424,7 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize)
} }
dbg_msg("mapchange", "imported settings"); dbg_msg("mapchange", "imported settings");
free(pSettings);
Reader.Close(); Reader.Close();
Writer.OpenFile(Storage(), aTemp); Writer.OpenFile(Storage(), aTemp);
Writer.Finish(); Writer.Finish();

View file

@ -31,7 +31,7 @@ void Process(IStorage *pStorage, const char *pMapName, const char *pConfigName)
} }
io_close(File); io_close(File);
pSettings = (char *)malloc(TotalLength); pSettings = (char *)malloc(maximum(1, TotalLength));
int Offset = 0; int Offset = 0;
for(int i = 0; i < aLines.size(); i++) 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) if(DataSize == TotalLength && mem_comp(pSettings, pMapSettings, DataSize) == 0)
{ {
dbg_msg("config_store", "configs coincide, not updating map"); dbg_msg("config_store", "configs coincide, not updating map");
free(pSettings);
return; return;
} }
Reader.UnloadData(pInfo->m_Settings); Reader.UnloadData(pInfo->m_Settings);
@ -121,6 +122,7 @@ void Process(IStorage *pStorage, const char *pMapName, const char *pConfigName)
Reader.UnloadData(i); Reader.UnloadData(i);
} }
free(pSettings);
Reader.Close(); Reader.Close();
if(!Writer.OpenFile(pStorage, pMapName)) if(!Writer.OpenFile(pStorage, pMapName))
{ {