summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2023-03-26 18:29:52 +0300
committerGeorge Hazan <ghazan@miranda.im>2023-03-26 18:29:52 +0300
commite772a0febc29babe3826011e226f20af0125aab6 (patch)
tree9a01ea37a63d089e3552a56da25730a765591c3c
parent2f6fc29c613c226d7a3b98fa20c79b2ae8ea1c0f (diff)
Telegram: fix for a possible crash
-rw-r--r--protocols/Telegram/src/avatars.cpp66
-rw-r--r--protocols/Telegram/src/proto.cpp2
-rw-r--r--protocols/Telegram/src/proto.h5
-rw-r--r--protocols/Telegram/src/utils.cpp11
4 files changed, 53 insertions, 31 deletions
diff --git a/protocols/Telegram/src/avatars.cpp b/protocols/Telegram/src/avatars.cpp
index 7e1d117ed7..66d0710cbc 100644
--- a/protocols/Telegram/src/avatars.cpp
+++ b/protocols/Telegram/src/avatars.cpp
@@ -77,6 +77,17 @@ INT_PTR CTelegramProto::SvcSetMyAvatar(WPARAM, LPARAM)
/////////////////////////////////////////////////////////////////////////////////////////
+TG_FILE_REQUEST* CTelegramProto::PopFile(const char *pszUniqueId)
+{
+ mir_cslock lck(m_csFiles);
+
+ for (auto &it : m_arFiles)
+ if (it->m_uniqueId == pszUniqueId)
+ return it;
+
+ return nullptr;
+}
+
void CTelegramProto::ProcessFile(TD::updateFile *pObj)
{
if (auto *pFile = pObj->file_.get()) {
@@ -85,38 +96,41 @@ void CTelegramProto::ProcessFile(TD::updateFile *pObj)
Utf2T wszExistingFile(pFile->local_->path_.c_str());
- for (auto &it : m_arFiles) {
- if (it->m_uniqueId == pFile->remote_->unique_id_.c_str()) {
- CMStringW wszFullName = it->m_destPath + L"\\" + it->m_fileName;
-
- if (it->m_type == it->AVATAR) {
- if (it->m_fileName.Right(5).MakeLower() == L".webp") {
- if (auto *pImage = FreeImage_LoadU(FIF_WEBP, wszExistingFile)) {
- wszFullName.Truncate(wszFullName.GetLength() - 5);
- wszFullName += L".png";
- FreeImage_SaveU(FIF_PNG, pImage, wszFullName);
- FreeImage_Unload(pImage);
- }
+ if (auto *F = PopFile(pFile->remote_->unique_id_.c_str())) {
+ CMStringW wszFullName = F->m_destPath + L"\\" + F->m_fileName;
+
+ if (F->m_type == F->AVATAR) {
+ if (F->m_fileName.Right(5).MakeLower() == L".webp") {
+ if (auto *pImage = FreeImage_LoadU(FIF_WEBP, wszExistingFile)) {
+ wszFullName.Truncate(wszFullName.GetLength() - 5);
+ wszFullName += L".png";
+ FreeImage_SaveU(FIF_PNG, pImage, wszFullName);
+ FreeImage_Unload(pImage);
}
- else MoveFileW(wszExistingFile, wszFullName);
-
- SMADD_CONT cont = { 1, m_szModuleName, wszFullName };
- CallService(MS_SMILEYADD_LOADCONTACTSMILEYS, 0, LPARAM(&cont));
- m_arFiles.removeItem(&it);
}
- else { // FILE
- it->pfts.currentFileProgress = pFile->local_->downloaded_size_;
- ProtoBroadcastAck(it->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, it, (LPARAM)&it->pfts);
+ else MoveFileW(wszExistingFile, wszFullName);
+
+ SMADD_CONT cont = { 1, m_szModuleName, wszFullName };
+ CallService(MS_SMILEYADD_LOADCONTACTSMILEYS, 0, LPARAM(&cont));
- if (pFile->local_->is_downloading_completed_) {
- MoveFileW(wszExistingFile, wszFullName);
- ProtoBroadcastAck(it->pfts.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, it);
+ mir_cslock lck(m_csFiles);
+ m_arFiles.remove(F);
+ }
+ else { // FILE
+ F->pfts.currentFileProgress = pFile->local_->downloaded_size_;
+ ProtoBroadcastAck(F->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, F, (LPARAM)&F->pfts);
- m_arFiles.removeItem(&it);
- }
+ if (pFile->local_->is_downloading_completed_) {
+ MoveFileW(wszExistingFile, wszFullName);
+ ProtoBroadcastAck(F->pfts.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, F);
+
+ mir_cslock lck(m_csFiles);
+ m_arFiles.remove(F);
}
- return;
}
+
+ delete F;
+ return;
}
for (auto &it : m_arUsers) {
diff --git a/protocols/Telegram/src/proto.cpp b/protocols/Telegram/src/proto.cpp
index 586d27feb9..6a82a00347 100644
--- a/protocols/Telegram/src/proto.cpp
+++ b/protocols/Telegram/src/proto.cpp
@@ -35,7 +35,7 @@ static int CompareSuperGroups(const TG_SUPER_GROUP *p1, const TG_SUPER_GROUP *p2
CTelegramProto::CTelegramProto(const char* protoName, const wchar_t* userName) :
PROTO<CTelegramProto>(protoName, userName),
m_impl(*this),
- m_arFiles(1),
+ m_arFiles(1, PtrKeySortT),
m_arChats(100, CompareChats),
m_arUsers(100, CompareUsers),
m_arRequests(10, CompareRequests),
diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h
index 42bf428802..4963a02cde 100644
--- a/protocols/Telegram/src/proto.h
+++ b/protocols/Telegram/src/proto.h
@@ -175,7 +175,10 @@ class CTelegramProto : public PROTO<CTelegramProto>
CMStringA m_szFullPhone;
OBJLIST<TG_REQUEST_BASE> m_arRequests;
- OBJLIST<TG_FILE_REQUEST> m_arFiles;
+
+ mir_cs m_csFiles;
+ LIST<TG_FILE_REQUEST> m_arFiles;
+ TG_FILE_REQUEST* PopFile(const char *pszUniqueId);
static INT_PTR CALLBACK EnterEmail(void *param);
static INT_PTR CALLBACK EnterEmailCode(void *param);
diff --git a/protocols/Telegram/src/utils.cpp b/protocols/Telegram/src/utils.cpp
index a9213d55ac..5686e1aad1 100644
--- a/protocols/Telegram/src/utils.cpp
+++ b/protocols/Telegram/src/utils.cpp
@@ -236,10 +236,12 @@ CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, TD::MessageContent *pBo
pRequest->pfts.flags = PFTS_UNICODE;
pRequest->pfts.hContact = pUser->hContact;
pRequest->pfts.currentFileSize = pFile->size_;
- m_arFiles.insert(pRequest);
+ {
+ mir_cslock lck(m_csFiles);
+ m_arFiles.insert(pRequest);
+ }
auto *pszFileName = pDoc->document_->file_name_.c_str();
-
PROTORECVFILE pre = {};
pre.fileCount = 1;
pre.timestamp = time(0);
@@ -273,7 +275,10 @@ CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, TD::MessageContent *pBo
CreateDirectoryW(pRequest->m_destPath, 0);
pRequest->m_fileName.Format(L"STK{%S}.%S", pFileId, pwszFileExt);
- m_arFiles.insert(pRequest);
+ {
+ mir_cslock lck(m_csFiles);
+ m_arFiles.insert(pRequest);
+ }
SendQuery(new TD::downloadFile(pFile->id_, 10, 0, 0, true));
return CMStringA(FORMAT, "STK{%s}", pFileId);