Skip to content

Commit

Permalink
Avoid undefined behaviour when deleting a void *
Browse files Browse the repository at this point in the history
  • Loading branch information
th-otto authored and OmniBlade committed Mar 20, 2024
1 parent d5d959a commit 1279a01
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion common/bfiofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ void BufferIOFileClass::Free(void)
{
if (Buffer) {
if (IsAllocated) {
delete[] Buffer;
delete[] static_cast<char*>(Buffer);
IsAllocated = false;
}

Expand Down
6 changes: 3 additions & 3 deletions common/mixfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ template <class T, class TCRC> bool MixFileClass<T, TCRC>::Cache(Buffer const* b
*/
int actual = straw->Get(Data, DataSize);
if (actual != DataSize) {
delete[] Data;
delete[] static_cast<char*>(Data);
Data = NULL;
file.Error(EIO);
return (false);
Expand All @@ -670,7 +670,7 @@ template <class T, class TCRC> bool MixFileClass<T, TCRC>::Cache(Buffer const* b
sha.Result(digest2);
fstraw.Get(digest1, sizeof(digest1));
if (memcmp(digest1, digest2, sizeof(digest1)) != 0) {
delete[] Data;
delete[] static_cast<char*>(Data);
Data = NULL;
return (false);
}
Expand Down Expand Up @@ -702,7 +702,7 @@ template <class T, class TCRC> bool MixFileClass<T, TCRC>::Cache(Buffer const* b
template <class T, class TCRC> void MixFileClass<T, TCRC>::Free(void)
{
if (Data != NULL && IsAllocated) {
delete[] Data;
delete[] static_cast<char*>(Data);
}
Data = NULL;
IsAllocated = false;
Expand Down
2 changes: 1 addition & 1 deletion redalert/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ void EventClass::Execute(void)
printf("%d\n", ((char*)Data.Variable.Pointer)[i]);
}
if (ID != PlayerPtr->ID) {
delete[] Data.Variable.Pointer;
delete[] static_cast<char*>(Data.Variable.Pointer);
}
break;

Expand Down
4 changes: 2 additions & 2 deletions redalert/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2910,7 +2910,7 @@ void Free_Heaps(void)
*/
for (int index = 0; index < ARRAY_SIZE(SpeechBuffer); index++) {
if (SpeechBuffer[index]) {
delete[] SpeechBuffer[index];
delete[] static_cast<char*>(SpeechBuffer[index]);
SpeechBuffer[index] = NULL;
}
}
Expand All @@ -2928,4 +2928,4 @@ void Free_Heaps(void)
delete InterpolationTable;
InterpolationTable = NULL;
}
}
}
2 changes: 1 addition & 1 deletion redalert/msgbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ int WWMessageBox::Process(const char* msg, const char* b1txt, const char* b2txt,
}
SeenBuff.Unlock();

delete[] back;
delete[] static_cast<char*>(back);
back = NULL;
Show_Mouse();
}
Expand Down
6 changes: 3 additions & 3 deletions redalert/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2870,7 +2870,7 @@ static int Extract_Uncompressed_Events(void* buf, int bufsize)

if (!DoList.Add(*event)) {
if (event->Type == EventClass::ADDPLAYER) {
delete[] event->Data.Variable.Pointer;
delete[] static_cast<char*>(event->Data.Variable.Pointer);
}
return (-1);
}
Expand Down Expand Up @@ -3035,7 +3035,7 @@ static int Extract_Compressed_Events(void* buf, int bufsize)

if (!DoList.Add(eventdata)) {
if (eventdata.Type == EventClass::ADDPLAYER) {
delete[] eventdata.Data.Variable.Pointer;
delete[] static_cast<char*>(eventdata.Data.Variable.Pointer);
}
return (-1);
}
Expand Down Expand Up @@ -4399,4 +4399,4 @@ void Check_Mirror(void)
#endif
} // end of Check_Mirror

/*************************** end of queue.cpp ******************************/
/*************************** end of queue.cpp ******************************/
2 changes: 1 addition & 1 deletion redalert/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ void Send_Statistics_Packet(void)
/*
** Tidy up
*/
delete[] packet;
delete[] static_cast<char*>(packet);

GameStatisticsPacketSent = true;
#endif // INTERNET_OFF
Expand Down
4 changes: 2 additions & 2 deletions tiberiandawn/ending.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ void Nod_Ending(void)

Play_Movie("CC2TEASE");

delete[] localpal;
delete[] static_cast<char*>(localpal);
delete TextPrintBuffer;
BlitList.Clear();
}
#endif
#endif
4 changes: 2 additions & 2 deletions tiberiandawn/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ void Uninit_Game(void)
{
Map.Free_Cells();

delete[] SpeechBuffer;
delete[] static_cast<char*>(SpeechBuffer);

CCFileClass::Clear_Search_Drives();
MFCD::Free_All();
Expand Down Expand Up @@ -2075,7 +2075,7 @@ void Parse_INI_File(void)
Fetch working pointer to the INI staging buffer. Make sure that the buffer
is cleared out before proceeding.
------------------------------------------------------------------------*/
buffer = (char*)_ShapeBuffer;
buffer = _ShapeBuffer;
memset(buffer, '\0', _ShapeBufferSize);

/*------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tiberiandawn/msgbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ int WWMessageBox::Process(const char* msg, const char* b1txt, const char* b2txt,
}
SeenBuff.Unlock();

delete[] back;
delete[] static_cast<char*>(back);
back = NULL;
Show_Mouse();
}
Expand Down
2 changes: 1 addition & 1 deletion tiberiandawn/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ void Send_Statistics_Packet(void)
** Tidy up
*/
CCDebugString("C&C95 - About to delete packet memory.\n");
delete[] packet;
delete[] static_cast<char*>(packet);

GameStatisticsPacketSent = true;
CCDebugString("C&C95 - Returning from Send_Statistics_Packet.\n");
Expand Down

0 comments on commit 1279a01

Please sign in to comment.