Skip to content

Commit

Permalink
Merge pull request #19467 from hrydgard/german-check-before-delete
Browse files Browse the repository at this point in the history
[Common/Core/Windows] Removed excess check pointer before delete or free
  • Loading branch information
hrydgard authored Sep 17, 2024
2 parents 635ff79 + 3c66f14 commit 9d48e51
Show file tree
Hide file tree
Showing 26 changed files with 46 additions and 96 deletions.
2 changes: 1 addition & 1 deletion Common/Data/Collections/FastVec.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FastVec {
data_ = (T *)malloc(initialCapacity * sizeof(T));
_assert_(data_ != nullptr);
}
~FastVec() { if (data_) free(data_); }
~FastVec() { free(data_); }

T &push_uninitialized() {
if (size_ < capacity_) {
Expand Down
3 changes: 1 addition & 2 deletions Common/Data/Format/JSONReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class JsonReader {
}

~JsonReader() {
if (buffer_)
free(buffer_);
free(buffer_);
}

bool ok() const { return ok_; }
Expand Down
4 changes: 1 addition & 3 deletions Common/GPU/OpenGL/GLRenderManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ class GLRProgram {
if (program) {
glDeleteProgram(program);
}
if (locData_) {
delete locData_;
}
delete locData_;
}
struct Semantic {
int location;
Expand Down
4 changes: 1 addition & 3 deletions Common/GPU/OpenGL/GLSLProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ struct AutoCharArrayBuf {
buf_ = nullptr;
}
void reset(char *buf) {
if (buf_) {
delete[] buf_;
}
delete[] buf_;
buf_ = buf;
}
operator char *() {
Expand Down
3 changes: 1 addition & 2 deletions Common/Log/LogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ LogManager::~LogManager() {
// Make sure we don't shutdown while logging. RemoveListener locks too, but there are gaps.
std::lock_guard<std::mutex> listeners_lock(listeners_lock_);

if (fileLog_)
delete fileLog_;
delete fileLog_;
#if !defined(MOBILE_DEVICE) || defined(_DEBUG)
#if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)
delete consoleLog_;
Expand Down
3 changes: 1 addition & 2 deletions Common/Serialize/SerializeFuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ void DoClass(PointerWrap &p, T &x) {
template<class T>
void DoClass(PointerWrap &p, T *&x) {
if (p.mode == PointerWrap::MODE_READ) {
if (x != nullptr)
delete x;
delete x;
x = new T();
}
x->DoState(p);
Expand Down
12 changes: 4 additions & 8 deletions Common/Serialize/SerializeMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ template<class K, class T>
void Do(PointerWrap &p, std::map<K, T *> &x) {
if (p.mode == PointerWrap::MODE_READ) {
for (auto it = x.begin(), end = x.end(); it != end; ++it) {
if (it->second != nullptr)
delete it->second;
delete it->second;
}
}
T *dv = nullptr;
Expand All @@ -81,8 +80,7 @@ template<class K, class T>
void Do(PointerWrap &p, std::unordered_map<K, T *> &x) {
if (p.mode == PointerWrap::MODE_READ) {
for (auto it = x.begin(), end = x.end(); it != end; ++it) {
if (it->second != nullptr)
delete it->second;
delete it->second;
}
}
T *dv = nullptr;
Expand Down Expand Up @@ -135,8 +133,7 @@ template<class K, class T>
void Do(PointerWrap &p, std::multimap<K, T *> &x) {
if (p.mode == PointerWrap::MODE_READ) {
for (auto it = x.begin(), end = x.end(); it != end; ++it) {
if (it->second != nullptr)
delete it->second;
delete it->second;
}
}
T *dv = nullptr;
Expand All @@ -153,8 +150,7 @@ template<class K, class T>
void Do(PointerWrap &p, std::unordered_multimap<K, T *> &x) {
if (p.mode == PointerWrap::MODE_READ) {
for (auto it = x.begin(), end = x.end(); it != end; ++it) {
if (it->second != nullptr)
delete it->second;
delete it->second;
}
}
T *dv = nullptr;
Expand Down
3 changes: 1 addition & 2 deletions Common/Serialize/SerializeSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ template <class T>
void Do(PointerWrap &p, std::set<T *> &x) {
if (p.mode == PointerWrap::MODE_READ) {
for (auto it = x.begin(), end = x.end(); it != end; ++it) {
if (*it != nullptr)
delete *it;
delete *it;
}
}
DoSet(p, x);
Expand Down
12 changes: 3 additions & 9 deletions Common/UI/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ void ScreenManager::switchToNext() {
}
stack_.push_back(nextStack_.front());
nextStack_.front().screen->focusChanged(ScreenFocusChange::FOCUS_BECAME_TOP);
if (temp.screen) {
delete temp.screen;
}
delete temp.screen;
UI::SetFocusedView(nullptr);

// When will this ever happen? Should handle focus here too?
Expand Down Expand Up @@ -404,15 +402,11 @@ void ScreenManager::processFinishDialog() {
}

void ScreenManager::SetBackgroundOverlayScreens(Screen *backgroundScreen, Screen *overlayScreen) {
if (backgroundScreen_) {
delete backgroundScreen_;
}
delete backgroundScreen_;
backgroundScreen_ = backgroundScreen;
backgroundScreen_->setScreenManager(this);

if (overlayScreen_) {
delete overlayScreen_;
}
delete overlayScreen_;
overlayScreen_ = overlayScreen;
overlayScreen_->setScreenManager(this);
}
10 changes: 4 additions & 6 deletions Core/Dialog/SavedataParam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1494,16 +1494,15 @@ void SavedataParam::Clear()
}

delete [] saveDataList;
saveDataList = 0;
saveDataList = NULL;
saveDataListCount = 0;
}
if (noSaveIcon)
{
if (noSaveIcon->texture != NULL)
delete noSaveIcon->texture;
delete noSaveIcon->texture;
noSaveIcon->texture = NULL;
delete noSaveIcon;
noSaveIcon = 0;
noSaveIcon = NULL;
}
}

Expand Down Expand Up @@ -1924,8 +1923,7 @@ void SavedataParam::DoState(PointerWrap &p) {
Do(p, saveDataListCount);
Do(p, saveNameListDataCount);
if (p.mode == p.MODE_READ) {
if (saveDataList)
delete [] saveDataList;
delete [] saveDataList;
if (saveDataListCount != 0) {
saveDataList = new SaveFileInfo[saveDataListCount];
DoArray(p, saveDataList, saveDataListCount);
Expand Down
3 changes: 1 addition & 2 deletions Core/ELF/ParamSFO.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ class ParamSFOData
void SetData(const u8* data, int size);

~ValueData() {
if (u_value)
delete[] u_value;
delete[] u_value;
}
};

Expand Down
8 changes: 2 additions & 6 deletions Core/Font/PGF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ PGF::PGF()
}

PGF::~PGF() {
if (fontData) {
delete [] fontData;
}
delete [] fontData;
}

struct GlyphFromPGF1State {
Expand Down Expand Up @@ -139,9 +137,7 @@ void PGF::DoState(PointerWrap &p) {
Do(p, fontDataSizeTemp);
fontDataSize = (size_t)fontDataSizeTemp;
if (p.mode == p.MODE_READ) {
if (fontData) {
delete [] fontData;
}
delete [] fontData;
if (fontDataSize) {
fontData = new u8[fontDataSize];
DoArray(p, fontData, (int)fontDataSize);
Expand Down
3 changes: 1 addition & 2 deletions Core/HLE/ThreadQueueList.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ struct ThreadQueueList {

inline void clear() {
for (int i = 0; i < NUM_QUEUES; ++i) {
if (queues[i].data != nullptr)
free(queues[i].data);
free(queues[i].data);
}
memset(queues, 0, sizeof(queues));
first = invalid();
Expand Down
3 changes: 1 addition & 2 deletions Core/HLE/sceKernelInterrupt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,7 @@ void __KernelReturnFromInterrupt()

void __RegisterIntrHandler(u32 intrNumber, IntrHandler* handler)
{
if(intrHandlers[intrNumber])
delete intrHandlers[intrNumber];
delete intrHandlers[intrNumber];
intrHandlers[intrNumber] = handler;
}

Expand Down
4 changes: 1 addition & 3 deletions Core/HLE/sceKernelMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ struct FPL : public KernelObject
{
FPL() : blocks(NULL), nextBlock(0) {}
~FPL() {
if (blocks != NULL) {
delete [] blocks;
}
delete [] blocks;
}
const char *GetName() override { return nf.name; }
const char *GetTypeName() override { return GetStaticTypeName(); }
Expand Down
16 changes: 5 additions & 11 deletions Core/HLE/sceKernelModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1387,8 +1387,7 @@ static PSPModule *__KernelLoadELFFromPtr(const u8 *ptr, size_t elfSize, u32 load
if (*magicPtr != 0x464c457f) {
ERROR_LOG(Log::sceModule, "Wrong magic number %08x", *magicPtr);
*error_string = "File corrupt";
if (newptr)
delete [] newptr;
delete [] newptr;
module->Cleanup();
kernelObjects.Destroy<PSPModule>(module->GetUID());
error = SCE_KERNEL_ERROR_UNSUPPORTED_PRX_TYPE;
Expand All @@ -1401,8 +1400,7 @@ static PSPModule *__KernelLoadELFFromPtr(const u8 *ptr, size_t elfSize, u32 load
int result = reader.LoadInto(loadAddress, fromTop);
if (result != SCE_KERNEL_ERROR_OK) {
ERROR_LOG(Log::sceModule, "LoadInto failed with error %08x",result);
if (newptr)
delete [] newptr;
delete [] newptr;
module->Cleanup();
kernelObjects.Destroy<PSPModule>(module->GetUID());
error = result;
Expand All @@ -1426,8 +1424,7 @@ static PSPModule *__KernelLoadELFFromPtr(const u8 *ptr, size_t elfSize, u32 load
if (!Memory::IsValidAddress(modinfoaddr)) {
*error_string = StringFromFormat("Bad module info address %08x", modinfoaddr);
ERROR_LOG(Log::sceModule, "Bad module info address %08x", modinfoaddr);
if (newptr)
delete[] newptr;
delete[] newptr;
module->Cleanup();
kernelObjects.Destroy<PSPModule>(module->GetUID());
error = SCE_KERNEL_ERROR_BAD_FILE; // Probably not the right error code.
Expand Down Expand Up @@ -1729,8 +1726,7 @@ static PSPModule *__KernelLoadELFFromPtr(const u8 *ptr, size_t elfSize, u32 load
module->nm.entry_addr = -1;
}

if (newptr)
delete [] newptr;
delete [] newptr;

if (!reportedModule && IsHLEVersionedModule(modinfo->name)) {
INFO_LOG(Log::sceModule, "Loading module %s with version %04x, devkit %08x", modinfo->name, modinfo->moduleVersion, devkitVersion);
Expand Down Expand Up @@ -1817,9 +1813,7 @@ static PSPModule *__KernelLoadModule(u8 *fileptr, size_t fileSize, SceKernelLMOp
u32 error;
module = __KernelLoadELFFromPtr(temp ? temp : fileptr + offsets[5], elfSize, PSP_GetDefaultLoadAddress(), false, error_string, &magic, error);

if (temp) {
delete [] temp;
}
delete [] temp;
} else if (fileSize > sizeof(PSP_Header)) {
u32 error;
u32 magic = 0;
Expand Down
2 changes: 1 addition & 1 deletion Core/HLE/sceNetAdhoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5008,7 +5008,7 @@ int NetAdhocMatching_Start(int matchingId, int evthPri, int evthPartitionId, int
//sceNetAdhocMatchingSetHelloOpt(matchingId, optLen, optDataAddr); //SetHelloOpt only works when context is running
if ((optLen > 0) && Memory::IsValidAddress(optDataAddr)) {
// Allocate the memory and copy the content
if (item->hello != NULL) free(item->hello);
free(item->hello);
item->hello = (uint8_t*)malloc(optLen);
if (item->hello != NULL) {
Memory::Memcpy(item->hello, optDataAddr, optLen);
Expand Down
3 changes: 1 addition & 2 deletions Core/HLE/scePsmf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ class PsmfPlayer {
PsmfPlayer(const PsmfPlayerCreateData *data);
~PsmfPlayer() {
AbortFinish();
if (mediaengine)
delete mediaengine;
delete mediaengine;
pspFileSystem.CloseFile(filehandle);
}
void DoState(PointerWrap &p);
Expand Down
6 changes: 2 additions & 4 deletions Core/HW/BufferQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ struct BufferQueue {
}

~BufferQueue() {
if (bufQueue)
delete [] bufQueue;
delete [] bufQueue;
}

bool alloc(int size) {
_assert_(size > 0);
if (bufQueue)
delete [] bufQueue;
delete [] bufQueue;
bufQueue = new unsigned char[size];
bufQueueSize = size;
clear();
Expand Down
6 changes: 2 additions & 4 deletions Core/HW/MediaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ MediaEngine::~MediaEngine() {

void MediaEngine::closeMedia() {
closeContext();
if (m_pdata)
delete m_pdata;
if (m_demux)
delete m_demux;
delete m_pdata;
delete m_demux;
m_pdata = nullptr;
m_demux = nullptr;
AudioClose(&m_audioContext);
Expand Down
2 changes: 1 addition & 1 deletion Core/HW/SasAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class VagDecoder {
class SasAtrac3 {
public:
SasAtrac3() : contextAddr_(0), atracID_(-1), sampleQueue_(0), end_(false) {}
~SasAtrac3() { if (sampleQueue_) delete sampleQueue_; }
~SasAtrac3() { delete sampleQueue_; }
int setContext(u32 context);
void getNextSamples(s16 *outbuf, int wantedSamples);
int addStreamData(u32 bufPtr, u32 addbytes);
Expand Down
5 changes: 2 additions & 3 deletions Core/MIPS/MIPSVFPUUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,10 +1000,9 @@ static inline bool load_vfpu_table(T *&ptr, const char *filename, size_t expecte
size_t size = 0u;
INFO_LOG(Log::CPU, "Loading '%s'...", filename);
ptr = reinterpret_cast<decltype(&*ptr)>(g_VFS.ReadFile(filename, &size));
if(!ptr || size != expected_size)
{
if (!ptr || size != expected_size) {
ERROR_LOG(Log::CPU, "Error loading '%s' (size=%u, expected: %u)", filename, (unsigned)size, (unsigned)expected_size);
if(ptr) delete[] ptr;
delete[] ptr;
ptr = nullptr;
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions GPU/Vulkan/ShaderManagerVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ static Promise<VkShaderModule> *CompileShaderModuleAsync(VulkanContext *vulkan,
#ifdef SHADERLOG
OutputDebugStringA("OK");
#endif
if (tag)
delete tag;
delete tag;
}
return shaderModule;
};
Expand Down
6 changes: 2 additions & 4 deletions Windows/Debugger/CtrlRegisterList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ CtrlRegisterList::CtrlRegisterList(HWND _wnd)
CtrlRegisterList::~CtrlRegisterList()
{
DeleteObject(font);
if (lastCat0Values != NULL)
delete [] lastCat0Values;
if (changedCat0Regs != NULL)
delete [] changedCat0Regs;
delete [] lastCat0Values;
delete [] changedCat0Regs;
}

void fillRect(HDC hdc, RECT *rect, COLORREF colour);
Expand Down
Loading

0 comments on commit 9d48e51

Please sign in to comment.