Alternative alternative fix for malloc of size 0

Has a short diff to the original code before a4f13ed4a8.
Supersedes #3353. Thanks for the idea, @timakro.
This commit is contained in:
heinrich5991 2020-12-08 12:29:10 +01:00
parent 56a90c25ec
commit e805e42a94
2 changed files with 18 additions and 28 deletions

View file

@ -529,12 +529,9 @@ void CDemoPlayer::ScanFile()
}
// copy all the frames to an array instead for fast access
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;
}
m_pKeyFrames = (CKeyFrame *)calloc(std::max(m_Info.m_SeekablePoints, 1), 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);
@ -1041,11 +1038,8 @@ int CDemoPlayer::Stop()
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "demo_player", "Stopped playback");
io_close(m_File);
m_File = 0;
if(m_pKeyFrames)
{
free(m_pKeyFrames);
m_pKeyFrames = 0;
}
free(m_pKeyFrames);
m_pKeyFrames = 0;
str_copy(m_aFilename, "", sizeof(m_aFilename));
return 0;
}

View file

@ -270,8 +270,7 @@ int CEditorMap::Save(class IStorage *pStorage, const char *pFileName)
Size += str_length(m_lSettings[i].m_aCommand) + 1;
}
// 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 *pSettings = (char *)malloc(std::max(Size, 1));
char *pNext = pSettings;
for(int i = 0; i < m_lSettings.size(); i++)
{
@ -535,24 +534,21 @@ int CEditorMap::Save(class IStorage *pStorage, const char *pFileName)
PointCount += Item.m_NumPoints;
}
if(PointCount > 0)
// save points
int TotalSize = sizeof(CEnvPoint) * PointCount;
CEnvPoint *pPoints = (CEnvPoint *)calloc(std::max(PointCount, 1), sizeof(*pPoints));
PointCount = 0;
for(int e = 0; e < m_lEnvelopes.size(); e++)
{
// 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++)
{
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);
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();
m_pEditor->Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "editor", "saving done");