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);
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);

View file

@ -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;
}

View file

@ -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)

View file

@ -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();

View file

@ -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();

View file

@ -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))
{