From d7c9eb34f80f207efd47d2fc65e31aedf166c323 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 5 Jun 2021 17:50:34 +0300 Subject: major code cleaning in regard to db_event_getBlobSize & event memory allocation --- protocols/Discord/src/dispatch.cpp | 6 ++---- protocols/Discord/src/proto.cpp | 10 ++++------ protocols/JabberG/src/jabber_proto.cpp | 24 ++++++------------------ protocols/JabberG/src/jabber_rc.cpp | 23 ++++++----------------- protocols/JabberG/src/jabber_thread.cpp | 17 +++++------------ protocols/SkypeWeb/src/skype_db.cpp | 10 +++++----- protocols/SkypeWeb/src/skype_proto.cpp | 12 ++++-------- protocols/Steam/src/steam_proto.cpp | 13 +++++-------- protocols/VKontakte/src/misc.cpp | 6 ++---- 9 files changed, 39 insertions(+), 82 deletions(-) (limited to 'protocols') diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index 4f2d25d35d..6cdef5e4de 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -390,9 +390,8 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) if (!bIsNew) { MEVENT hOldEvent = db_event_getById(m_szModuleName, szMsgId); if (hOldEvent) { - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hOldEvent); - dbei.pBlob = (BYTE*)mir_alloc(dbei.cbBlob); + DB::EventInfo dbei; + dbei.cbBlob = -1; if (!db_event_get(hOldEvent, &dbei)) { ptrW wszOldText(DbEvent_GetTextW(&dbei, CP_UTF8)); if (wszOldText) @@ -400,7 +399,6 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) if (dbei.flags & DBEF_SENT) bOurMessage = true; } - mir_free(dbei.pBlob); } } diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index c8304b6375..e5a62df682 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -374,9 +374,8 @@ int CDiscordProto::AuthRecv(MCONTACT, PROTORECVEVENT *pre) int CDiscordProto::Authorize(MEVENT hDbEvent) { - DBEVENTINFO dbei = {}; - if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1)) return 1; - if ((dbei.pBlob = (PBYTE)alloca(dbei.cbBlob)) == nullptr) return 1; + DB::EventInfo dbei; + dbei.cbBlob = -1; if (db_event_get(hDbEvent, &dbei)) return 1; if (dbei.eventType != EVENTTYPE_AUTHREQUEST) return 1; if (mir_strcmp(dbei.szModule, m_szModuleName)) return 1; @@ -390,9 +389,8 @@ int CDiscordProto::Authorize(MEVENT hDbEvent) int CDiscordProto::AuthDeny(MEVENT hDbEvent, const wchar_t*) { - DBEVENTINFO dbei = {}; - if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1)) return 1; - if ((dbei.pBlob = (PBYTE)alloca(dbei.cbBlob)) == nullptr) return 1; + DB::EventInfo dbei; + dbei.cbBlob = -1; if (db_event_get(hDbEvent, &dbei)) return 1; if (dbei.eventType != EVENTTYPE_AUTHREQUEST) return 1; if (mir_strcmp(dbei.szModule, m_szModuleName)) return 1; diff --git a/protocols/JabberG/src/jabber_proto.cpp b/protocols/JabberG/src/jabber_proto.cpp index 1ab7f3588a..cb222192d8 100755 --- a/protocols/JabberG/src/jabber_proto.cpp +++ b/protocols/JabberG/src/jabber_proto.cpp @@ -383,11 +383,8 @@ MCONTACT CJabberProto::AddToListByEvent(int flags, int /*iContact*/, MEVENT hDbE { debugLogA("AddToListByEvent"); - DBEVENTINFO dbei = {}; - if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1)) - return 0; - if ((dbei.pBlob = (PBYTE)alloca(dbei.cbBlob)) == nullptr) - return 0; + DB::EventInfo dbei; + dbei.cbBlob = -1; if (db_event_get(hDbEvent, &dbei)) return 0; if (mir_strcmp(dbei.szModule, m_szModuleName)) @@ -407,11 +404,8 @@ int CJabberProto::Authorize(MEVENT hDbEvent) if (!m_bJabberOnline) return 1; - DBEVENTINFO dbei = {}; - if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1)) - return 1; - if ((dbei.pBlob = (PBYTE)alloca(dbei.cbBlob)) == nullptr) - return 1; + DB::EventInfo dbei; + dbei.cbBlob = -1; if (db_event_get(hDbEvent, &dbei)) return 1; if (dbei.eventType != EVENTTYPE_AUTHREQUEST) @@ -450,14 +444,8 @@ int CJabberProto::AuthDeny(MEVENT hDbEvent, const wchar_t*) debugLogA("Entering AuthDeny"); - DBEVENTINFO dbei = {}; - if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1)) - return 1; - - mir_ptr pBlob((PBYTE)mir_alloc(dbei.cbBlob)); - if ((dbei.pBlob = pBlob) == nullptr) - return 1; - + DB::EventInfo dbei; + dbei.cbBlob = -1; if (db_event_get(hDbEvent, &dbei)) return 1; diff --git a/protocols/JabberG/src/jabber_rc.cpp b/protocols/JabberG/src/jabber_rc.cpp index 8e559e2594..0a1f196cfd 100644 --- a/protocols/JabberG/src/jabber_rc.cpp +++ b/protocols/JabberG/src/jabber_rc.cpp @@ -449,21 +449,15 @@ int CJabberProto::RcGetUnreadEventsCount() if (jid == nullptr) continue; for (MEVENT hDbEvent = db_event_firstUnread(hContact); hDbEvent; hDbEvent = db_event_next(hContact, hDbEvent)) { - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - if (dbei.cbBlob == -1) - continue; + DB::EventInfo dbei; + dbei.cbBlob = -1; - dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob + 1); int nGetTextResult = db_event_get(hDbEvent, &dbei); if (!nGetTextResult && dbei.eventType == EVENTTYPE_MESSAGE && !(dbei.flags & DBEF_READ) && !(dbei.flags & DBEF_SENT)) { - wchar_t *szEventText = DbEvent_GetTextW(&dbei, CP_ACP); - if (szEventText) { + ptrW szEventText(DbEvent_GetTextW(&dbei, CP_ACP)); + if (szEventText) nEventsSent++; - mir_free(szEventText); - } } - mir_free(dbei.pBlob); } } return nEventsSent; @@ -531,13 +525,8 @@ int CJabberProto::AdhocForwardHandler(const TiXmlElement*, CJabberIqInfo *pInfo, continue; for (MEVENT hDbEvent = db_event_firstUnread(hContact); hDbEvent; hDbEvent = db_event_next(hContact, hDbEvent)) { - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - if (dbei.cbBlob == -1) - continue; - - mir_ptr pEventBuf((PBYTE)mir_alloc(dbei.cbBlob + 1)); - dbei.pBlob = pEventBuf; + DB::EventInfo dbei; + dbei.cbBlob = -1; if (db_event_get(hDbEvent, &dbei)) continue; diff --git a/protocols/JabberG/src/jabber_thread.cpp b/protocols/JabberG/src/jabber_thread.cpp index bd85a439c9..7e4a20b731 100755 --- a/protocols/JabberG/src/jabber_thread.cpp +++ b/protocols/JabberG/src/jabber_thread.cpp @@ -980,18 +980,11 @@ DWORD JabberGetLastContactMessageTime(MCONTACT hContact) if (!hDbEvent) return 0; - DWORD dwTime = 0; - - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - if (dbei.cbBlob != -1) { - dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob + 1); - int nGetTextResult = db_event_get(hDbEvent, &dbei); - if (!nGetTextResult) - dwTime = dbei.timestamp; - mir_free(dbei.pBlob); - } - return dwTime; + DB::EventInfo dbei; + if (!db_event_get(hDbEvent, &dbei)) + return dbei.timestamp; + + return 0; } MCONTACT CJabberProto::CreateTemporaryContact(const char *szJid, JABBER_LIST_ITEM* chatItem) diff --git a/protocols/SkypeWeb/src/skype_db.cpp b/protocols/SkypeWeb/src/skype_db.cpp index 0dc8e8ce8b..e8ac1ab738 100644 --- a/protocols/SkypeWeb/src/skype_db.cpp +++ b/protocols/SkypeWeb/src/skype_db.cpp @@ -58,11 +58,11 @@ MEVENT CSkypeProto::AddDbEvent(WORD type, MCONTACT hContact, DWORD timestamp, DW void CSkypeProto::EditEvent(MCONTACT hContact, MEVENT hEvent, const CMStringW &szContent, time_t edit_time) { mir_cslock lck(m_AppendMessageLock); - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hEvent); - mir_ptr blob((PBYTE)mir_alloc(dbei.cbBlob)); - dbei.pBlob = blob; - db_event_get(hEvent, &dbei); + + DB::EventInfo dbei; + dbei.cbBlob = -1; + if (db_event_get(hEvent, &dbei)) + return; JSONNode jMsg = JSONNode::parse((char*)dbei.pBlob); if (jMsg) { diff --git a/protocols/SkypeWeb/src/skype_proto.cpp b/protocols/SkypeWeb/src/skype_proto.cpp index f5cc5f5bd9..6bc6e8e66e 100644 --- a/protocols/SkypeWeb/src/skype_proto.cpp +++ b/protocols/SkypeWeb/src/skype_proto.cpp @@ -192,11 +192,9 @@ MCONTACT CSkypeProto::AddToList(int, PROTOSEARCHRESULT *psr) MCONTACT CSkypeProto::AddToListByEvent(int, int, MEVENT hDbEvent) { debugLogA(__FUNCTION__); - DBEVENTINFO dbei = {}; - if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1)) - return NULL; - if ((dbei.pBlob = (PBYTE)alloca(dbei.cbBlob)) == nullptr) - return NULL; + + DB::EventInfo dbei; + dbei.cbBlob = -1; if (db_event_get(hDbEvent, &dbei)) return NULL; if (mir_strcmp(dbei.szModule, m_szModuleName)) @@ -205,9 +203,7 @@ MCONTACT CSkypeProto::AddToListByEvent(int, int, MEVENT hDbEvent) return NULL; DB::AUTH_BLOB blob(dbei.pBlob); - - MCONTACT hContact = AddContact(blob.get_email(), blob.get_nick()); - return hContact; + return AddContact(blob.get_email(), blob.get_nick()); } int CSkypeProto::Authorize(MEVENT hDbEvent) diff --git a/protocols/Steam/src/steam_proto.cpp b/protocols/Steam/src/steam_proto.cpp index 35bef1b6db..4b05b6c32d 100644 --- a/protocols/Steam/src/steam_proto.cpp +++ b/protocols/Steam/src/steam_proto.cpp @@ -109,17 +109,14 @@ MCONTACT CSteamProto::AddToList(int, PROTOSEARCHRESULT *psr) MCONTACT CSteamProto::AddToListByEvent(int, int, MEVENT hDbEvent) { - DBEVENTINFO dbei = {}; - if ((dbei.cbBlob = db_event_getBlobSize(hDbEvent)) == (DWORD)(-1)) - return NULL; - if ((dbei.pBlob = (PBYTE)alloca(dbei.cbBlob)) == nullptr) - return NULL; + DB::EventInfo dbei; + dbei.cbBlob = -1; if (db_event_get(hDbEvent, &dbei)) - return NULL; + return 0; if (mir_strcmp(dbei.szModule, m_szModuleName)) - return NULL; + return 0; if (dbei.eventType != EVENTTYPE_AUTHREQUEST) - return NULL; + return 0; DB::AUTH_BLOB blob(dbei.pBlob); return AddContact(blob.get_email(), Utf2T(blob.get_nick())); diff --git a/protocols/VKontakte/src/misc.cpp b/protocols/VKontakte/src/misc.cpp index 3859a1696c..2f3d8f74e6 100644 --- a/protocols/VKontakte/src/misc.cpp +++ b/protocols/VKontakte/src/misc.cpp @@ -1739,10 +1739,8 @@ MEVENT CVkProto::GetMessageFromDb(const char *messageId, UINT ×tamp, CMStri if (!hDbEvent) return 0; - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - mir_ptr blob((PBYTE)mir_alloc(dbei.cbBlob)); - dbei.pBlob = blob; + DB::EventInfo dbei; + dbei.cbBlob = -1; db_event_get(hDbEvent, &dbei); msg = ptrW(mir_utf8decodeW((char*)dbei.pBlob)); -- cgit v1.2.3