From 6eb51239c95bb3e8959ec15a1bdcf1fa245c9042 Mon Sep 17 00:00:00 2001 From: Learath2 Date: Fri, 12 Jan 2024 23:17:16 +0300 Subject: [PATCH] Fix some clang-tidy issues and UB --- src/engine/client/serverbrowser_http.cpp | 10 ++++------ src/engine/client/updater.cpp | 8 +++++--- src/engine/client/updater.h | 16 ++++++++-------- src/engine/shared/http.cpp | 11 ++++++++++- src/engine/shared/http.h | 3 ++- src/game/client/components/menus.h | 1 + src/game/client/components/menus_browser.cpp | 6 ++++++ 7 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/engine/client/serverbrowser_http.cpp b/src/engine/client/serverbrowser_http.cpp index 8d9734519..817428dc5 100644 --- a/src/engine/client/serverbrowser_http.cpp +++ b/src/engine/client/serverbrowser_http.cpp @@ -54,8 +54,8 @@ private: CChooseMaster *m_pParent; CLock m_Lock; std::shared_ptr m_pData; - std::shared_ptr m_pHead PT_GUARDED_BY(m_Lock); - std::shared_ptr m_pGet PT_GUARDED_BY(m_Lock); + std::shared_ptr m_pHead; + std::shared_ptr m_pGet; void Run() override REQUIRES(!m_Lock); public: @@ -174,7 +174,7 @@ void CChooseMaster::CJob::Run() { aTimeMs[i] = -1; const char *pUrl = m_pData->m_aaUrls[aRandomized[i]]; - std::shared_ptr pHead = std::move(HttpHead(pUrl)); + std::shared_ptr pHead = HttpHead(pUrl); pHead->Timeout(Timeout); pHead->LogProgress(HTTPLOG::FAILURE); { @@ -195,7 +195,7 @@ void CChooseMaster::CJob::Run() } auto StartTime = time_get_nanoseconds(); - std::shared_ptr pGet = std::move(HttpGet(pUrl)); + std::shared_ptr pGet = HttpGet(pUrl); pGet->Timeout(Timeout); pGet->LogProgress(HTTPLOG::FAILURE); { @@ -296,7 +296,6 @@ private: static bool Validate(json_value *pJson); static bool Parse(json_value *pJson, std::vector *pvServers, std::vector *pvLegacyServers); - IEngine *m_pEngine; IConsole *m_pConsole; IHttp *m_pHttp; @@ -309,7 +308,6 @@ private: }; CServerBrowserHttp::CServerBrowserHttp(IEngine *pEngine, IConsole *pConsole, IHttp *pHttp, const char **ppUrls, int NumUrls, int PreviousBestIndex) : - m_pEngine(pEngine), m_pConsole(pConsole), m_pHttp(pHttp), m_pChooseMaster(new CChooseMaster(pEngine, pHttp, Validate, ppUrls, NumUrls, PreviousBestIndex)) diff --git a/src/engine/client/updater.cpp b/src/engine/client/updater.cpp index eb238a709..3d1487e8f 100644 --- a/src/engine/client/updater.cpp +++ b/src/engine/client/updater.cpp @@ -163,7 +163,8 @@ bool CUpdater::MoveFile(const char *pFile) void CUpdater::Update() { - switch(m_State) + auto State = GetCurrentState(); + switch(State) { case IUpdater::GOT_MANIFEST: PerformUpdate(); @@ -270,6 +271,7 @@ void CUpdater::ParseUpdate() break; } } + json_value_free(pVersions); } void CUpdater::InitiateUpdate() @@ -375,9 +377,9 @@ void CUpdater::CommitUpdate() if(m_ServerUpdate) Success &= ReplaceServer(); if(!Success) - m_State = FAIL; + SetCurrentState(IUpdater::FAIL); else if(m_pClient->State() == IClient::STATE_ONLINE || m_pClient->EditorHasUnsavedData()) - m_State = NEED_RESTART; + SetCurrentState(IUpdater::NEED_RESTART); else { m_pClient->Restart(); diff --git a/src/engine/client/updater.h b/src/engine/client/updater.h index eb7e5f222..68148f161 100644 --- a/src/engine/client/updater.h +++ b/src/engine/client/updater.h @@ -48,7 +48,7 @@ class CUpdater : public IUpdater CLock m_Lock; - int m_State; + int m_State GUARDED_BY(m_Lock); char m_aStatus[256] GUARDED_BY(m_Lock); int m_Percent GUARDED_BY(m_Lock); char m_aClientExecTmp[64]; @@ -62,13 +62,13 @@ class CUpdater : public IUpdater bool m_ServerUpdate; void AddFileJob(const char *pFile, bool Job); - void FetchFile(const char *pFile, const char *pDestPath = nullptr); + void FetchFile(const char *pFile, const char *pDestPath = nullptr) REQUIRES(!m_Lock); bool MoveFile(const char *pFile); - void ParseUpdate(); - void PerformUpdate(); - void RunningUpdate(); - void CommitUpdate(); + void ParseUpdate() REQUIRES(!m_Lock); + void PerformUpdate() REQUIRES(!m_Lock); + void RunningUpdate() REQUIRES(!m_Lock); + void CommitUpdate() REQUIRES(!m_Lock); bool ReplaceClient(); bool ReplaceServer(); @@ -82,9 +82,9 @@ public: void GetCurrentFile(char *pBuf, int BufSize) override REQUIRES(!m_Lock); int GetCurrentPercent() override REQUIRES(!m_Lock); - void InitiateUpdate() override; + void InitiateUpdate() REQUIRES(!m_Lock) override; void Init(CHttp *pHttp); - void Update() override; + void Update() REQUIRES(!m_Lock) override; }; #endif diff --git a/src/engine/shared/http.cpp b/src/engine/shared/http.cpp index c866a7590..d87d76230 100644 --- a/src/engine/shared/http.cpp +++ b/src/engine/shared/http.cpp @@ -571,7 +571,7 @@ void CHttp::Run(std::shared_ptr pRequest) { std::unique_lock Lock(m_Lock); m_Cv.wait(Lock, [this]() { return m_State != UNINITIALIZED; }); - m_PendingRequests.emplace_back(std::move(std::static_pointer_cast(pRequest))); + m_PendingRequests.emplace_back(std::static_pointer_cast(pRequest)); curl_multi_wakeup(m_pMultiH); } @@ -584,3 +584,12 @@ void CHttp::Shutdown() m_Shutdown = true; curl_multi_wakeup(m_pMultiH); } + +CHttp::~CHttp() +{ + if(!m_pThread) + return; + + Shutdown(); + thread_wait(m_pThread); +} diff --git a/src/engine/shared/http.h b/src/engine/shared/http.h index b87d53d22..599872fef 100644 --- a/src/engine/shared/http.h +++ b/src/engine/shared/http.h @@ -132,7 +132,7 @@ protected: public: CHttpRequest(const char *pUrl); - ~CHttpRequest(); + virtual ~CHttpRequest(); void Timeout(CTimeout Timeout) { m_Timeout = Timeout; } void MaxResponseSize(int64_t MaxResponseSize) { m_MaxResponseSize = MaxResponseSize; } @@ -273,6 +273,7 @@ public: // User virtual void Run(std::shared_ptr pRequest) override; void Shutdown() override; + ~CHttp(); }; #endif // ENGINE_SHARED_HTTP_H diff --git a/src/game/client/components/menus.h b/src/game/client/components/menus.h index 6f45fd405..6f34b2456 100644 --- a/src/game/client/components/menus.h +++ b/src/game/client/components/menus.h @@ -536,6 +536,7 @@ protected: public: CCommunityIconLoadJob(CMenus *pMenus, const char *pCommunityId, int StorageType); + ~CCommunityIconLoadJob(); CImageInfo &&ImageInfo() { return std::move(m_ImageInfo); } }; diff --git a/src/game/client/components/menus_browser.cpp b/src/game/client/components/menus_browser.cpp index 1a1df4321..c2f508c40 100644 --- a/src/game/client/components/menus_browser.cpp +++ b/src/game/client/components/menus_browser.cpp @@ -1822,6 +1822,12 @@ CMenus::CCommunityIconLoadJob::CCommunityIconLoadJob(CMenus *pMenus, const char { } +CMenus::CCommunityIconLoadJob::~CCommunityIconLoadJob() +{ + free(m_ImageInfo.m_pData); + m_ImageInfo.m_pData = nullptr; +} + int CMenus::CommunityIconScan(const char *pName, int IsDir, int DirType, void *pUser) { const char *pExtension = ".png";