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 --- plugins/AutoShutdown/src/watcher.cpp | 28 ++++++------ plugins/AvatarHistory/src/AvatarDlg.cpp | 12 ++--- plugins/Boltun/src/boltun.cpp | 16 ++----- plugins/ContactsPlus/src/main.cpp | 45 +++++++++--------- plugins/ContactsPlus/src/receive.cpp | 11 ++--- plugins/Db3x_mmap/src/dbevents.cpp | 9 +++- plugins/Dbx_mdbx/src/dbevents.cpp | 13 ++++-- plugins/Dbx_sqlite/src/dbevents.cpp | 7 ++- plugins/HistoryLinkListPlus/src/linklist.cpp | 7 +-- plugins/HistoryLinkListPlus/src/linklist_fct.cpp | 58 +++++++++--------------- plugins/IEView/src/HTMLBuilder.cpp | 12 ++--- plugins/Import/src/dbrw/dbevents.cpp | 16 +++++-- plugins/MirLua/src/Modules/m_database.cpp | 6 +-- plugins/MirandaG15/src/CAppletManager.cpp | 27 +++-------- plugins/Msg_Export/src/utils.cpp | 17 ++----- plugins/NewEventNotify/src/popup.cpp | 26 ++++------- plugins/NewStory/src/history.cpp | 15 +----- plugins/Scriver/src/msgdialog.cpp | 12 ++--- plugins/Scriver/src/msglog.cpp | 13 ++---- plugins/StopSpamMod/src/stopspam.cpp | 9 ++-- plugins/StopSpamPlus/src/events.cpp | 10 ++-- plugins/TabSRMM/src/eventpopups.cpp | 22 +++------ plugins/TabSRMM/src/msglog.cpp | 6 +-- plugins/Variables/src/parse_miranda.cpp | 13 ++---- plugins/YARelay/src/main.cpp | 8 +--- plugins/wbOSD/src/events.cpp | 13 ++---- 26 files changed, 162 insertions(+), 269 deletions(-) (limited to 'plugins') diff --git a/plugins/AutoShutdown/src/watcher.cpp b/plugins/AutoShutdown/src/watcher.cpp index e30ab9ad5f..b01cb95735 100644 --- a/plugins/AutoShutdown/src/watcher.cpp +++ b/plugins/AutoShutdown/src/watcher.cpp @@ -91,23 +91,21 @@ static wchar_t* GetMessageText(BYTE **ppBlob, DWORD *pcbBlob) static int MsgEventAdded(WPARAM, LPARAM hDbEvent) { if (currentWatcherType & SDWTF_MESSAGE) { - DBEVENTINFO dbe = {}; - dbe.cbBlob = db_event_getBlobSize(hDbEvent); - dbe.pBlob = (BYTE*)mir_alloc(dbe.cbBlob + 2); /* ensure term zero */ - if (dbe.pBlob == nullptr) + DB::EventInfo dbei; + dbei.cbBlob = -1; + if (db_event_get(hDbEvent, &dbei)) return 0; - if (!db_event_get(hDbEvent, &dbe)) - if (dbe.eventType == EVENTTYPE_MESSAGE && !(dbe.flags & DBEF_SENT)) { - DBVARIANT dbv; - if (!g_plugin.getWString("Message", &dbv)) { - TrimString(dbv.pwszVal); - wchar_t *pszMsg = GetMessageText(&dbe.pBlob, &dbe.cbBlob); - if (pszMsg != nullptr && wcsstr(pszMsg, dbv.pwszVal) != nullptr) - ShutdownAndStopWatcher(); /* msg with specified text recvd */ - mir_free(dbv.pwszVal); /* does NULL check */ - } + + if (dbei.eventType == EVENTTYPE_MESSAGE && !(dbei.flags & DBEF_SENT)) { + DBVARIANT dbv; + if (!g_plugin.getWString("Message", &dbv)) { + TrimString(dbv.pwszVal); + wchar_t *pszMsg = GetMessageText(&dbei.pBlob, &dbei.cbBlob); + if (pszMsg != nullptr && wcsstr(pszMsg, dbv.pwszVal) != nullptr) + ShutdownAndStopWatcher(); /* msg with specified text recvd */ + mir_free(dbv.pwszVal); /* does NULL check */ } - mir_free(dbe.pBlob); + } } return 0; } diff --git a/plugins/AvatarHistory/src/AvatarDlg.cpp b/plugins/AvatarHistory/src/AvatarDlg.cpp index 245c58005a..a03378ec28 100644 --- a/plugins/AvatarHistory/src/AvatarDlg.cpp +++ b/plugins/AvatarHistory/src/AvatarDlg.cpp @@ -412,12 +412,12 @@ int FillAvatarListFromDB(HWND list, MCONTACT hContact) int max_pos = 0; DB::ECPTR pCursor(DB::Events(hContact)); while (MEVENT hDbEvent = pCursor.FetchNext()) { - DBEVENTINFO dbei = {}; - BYTE blob[2048]; - dbei.cbBlob = sizeof(blob); - dbei.pBlob = blob; - if (db_event_get(hDbEvent, &dbei) != 0) continue; - if (dbei.eventType != EVENTTYPE_AVATAR_CHANGE) continue; + DB::EventInfo dbei; + dbei.cbBlob = -1; + if (db_event_get(hDbEvent, &dbei)) + continue; + if (dbei.eventType != EVENTTYPE_AVATAR_CHANGE) + continue; // Get time wchar_t date[64]; diff --git a/plugins/Boltun/src/boltun.cpp b/plugins/Boltun/src/boltun.cpp index 82a9994073..f764e3fc84 100644 --- a/plugins/Boltun/src/boltun.cpp +++ b/plugins/Boltun/src/boltun.cpp @@ -195,26 +195,16 @@ static int MessageEventAdded(WPARAM hContact, LPARAM hDbEvent) if (!BoltunAutoChat(hContact)) return 0; - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - if (dbei.cbBlob == -1) - return 0; - - dbei.pBlob = (PBYTE)malloc(dbei.cbBlob); - if (dbei.pBlob == nullptr) - return 0; - + DB::EventInfo dbei; + dbei.cbBlob = -1; db_event_get(hDbEvent, &dbei); if (dbei.flags & DBEF_SENT || dbei.flags & DBEF_READ || dbei.eventType != EVENTTYPE_MESSAGE) return 0; - wchar_t *s = DbEvent_GetTextW(&dbei, CP_ACP); - free(dbei.pBlob); if (Config.MarkAsRead) db_event_markRead(hContact, hDbEvent); - AnswerToContact(hContact, s); - mir_free(s); + AnswerToContact(hContact, ptrW(DbEvent_GetTextW(&dbei, CP_ACP))); return 0; } diff --git a/plugins/ContactsPlus/src/main.cpp b/plugins/ContactsPlus/src/main.cpp index 1b759ce240..905eaab7e3 100644 --- a/plugins/ContactsPlus/src/main.cpp +++ b/plugins/ContactsPlus/src/main.cpp @@ -56,33 +56,30 @@ CMPlugin::CMPlugin() : static int HookDBEventAdded(WPARAM hContact, LPARAM hDbEvent) { - //process the event + // process the event DBEVENTINFO dbe = {}; db_event_get(hDbEvent, &dbe); - //check if we should process the event - if (dbe.flags & (DBEF_SENT | DBEF_READ) || dbe.eventType != EVENTTYPE_CONTACTS) return 0; - //get event contents - dbe.cbBlob = db_event_getBlobSize(hDbEvent); - if (dbe.cbBlob != -1) - dbe.pBlob = (PBYTE)_alloca(dbe.cbBlob); - db_event_get(hDbEvent, &dbe); - //play received sound + + // check if we should process the event + if (dbe.flags & (DBEF_SENT | DBEF_READ) || dbe.eventType != EVENTTYPE_CONTACTS) + return 0; + + // play received sound Skin_PlaySound("RecvContacts"); - { - //add event to the contact list - wchar_t caToolTip[128]; - mir_snwprintf(caToolTip, L"%s %s", TranslateT("Contacts received from"), Clist_GetContactDisplayName(hContact)); - - CLISTEVENT cle = {}; - cle.hContact = hContact; - cle.hDbEvent = hDbEvent; - cle.hIcon = LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_CONTACTS)); - cle.pszService = MS_CONTACTS_RECEIVE; - cle.szTooltip.w = caToolTip; - cle.flags |= CLEF_UNICODE; - g_clistApi.pfnAddEvent(&cle); - } - return 0; //continue processing by other hooks + + // add event to the contact list + wchar_t caToolTip[128]; + mir_snwprintf(caToolTip, L"%s %s", TranslateT("Contacts received from"), Clist_GetContactDisplayName(hContact)); + + CLISTEVENT cle = {}; + cle.hContact = hContact; + cle.hDbEvent = hDbEvent; + cle.hIcon = LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_CONTACTS)); + cle.pszService = MS_CONTACTS_RECEIVE; + cle.szTooltip.w = caToolTip; + cle.flags |= CLEF_UNICODE; + g_clistApi.pfnAddEvent(&cle); + return 0; } static void ProcessUnreadEvents(void) diff --git a/plugins/ContactsPlus/src/receive.cpp b/plugins/ContactsPlus/src/receive.cpp index f4a0b1335d..da8b2230f3 100644 --- a/plugins/ContactsPlus/src/receive.cpp +++ b/plugins/ContactsPlus/src/receive.cpp @@ -195,11 +195,9 @@ INT_PTR CALLBACK RecvDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPara CheckDlgButton(hwndDlg, IDC_ENABLEGROUPS, BST_UNCHECKED); RebuildGroupCombo(hwndDlg); - { // fill listview with received contacts - DBEVENTINFO dbe = {}; - dbe.cbBlob = db_event_getBlobSize(wndData->mhDbEvent); - if (dbe.cbBlob != -1) // this marks an invalid hDbEvent - all smashed anyway... - dbe.pBlob = (PBYTE)_alloca(dbe.cbBlob); + { // fill listview with received contacts + DB::EventInfo dbe; + dbe.cbBlob = -1; db_event_get(wndData->mhDbEvent, &dbe); char* pcBlob = (char*)dbe.pBlob; char* pcEnd = (char*)dbe.pBlob + dbe.cbBlob; @@ -212,8 +210,7 @@ INT_PTR CALLBACK RecvDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lPara lvi.iImage = 0; lvi.mask = LVIF_TEXT | LVIF_IMAGE; - for (int nItem = 0; ; nItem++) - { // Nick + for (int nItem = 0; ; nItem++) { // Nick int strsize = (int)strlennull(pcBlob); TReceivedItem* pItem = wndData->AddReceivedItem(); diff --git a/plugins/Db3x_mmap/src/dbevents.cpp b/plugins/Db3x_mmap/src/dbevents.cpp index e3b91a52be..68384fa348 100644 --- a/plugins/Db3x_mmap/src/dbevents.cpp +++ b/plugins/Db3x_mmap/src/dbevents.cpp @@ -292,7 +292,14 @@ BOOL CDb3Mmap::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei) dbei->timestamp = dbe->timestamp; dbei->flags = dbe->flags; dbei->eventType = dbe->wEventType; - int bytesToCopy = (dbei->cbBlob < dbe->cbBlob) ? dbei->cbBlob : dbe->cbBlob; + + DWORD cbBlob = dbe->cbBlob; + size_t bytesToCopy = cbBlob; + if (dbei->cbBlob == -1) + dbei->pBlob = (PBYTE)mir_calloc(cbBlob + 2); + else if (dbei->cbBlob < cbBlob) + bytesToCopy = dbei->cbBlob; + dbei->cbBlob = dbe->cbBlob; if (bytesToCopy && dbei->pBlob) { BYTE *pSrc; diff --git a/plugins/Dbx_mdbx/src/dbevents.cpp b/plugins/Dbx_mdbx/src/dbevents.cpp index 2c9a1d71f0..aa7f634f64 100644 --- a/plugins/Dbx_mdbx/src/dbevents.cpp +++ b/plugins/Dbx_mdbx/src/dbevents.cpp @@ -296,7 +296,6 @@ BOOL CDbxMDBX::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei) return 1; } - size_t cbBlob; const DBEvent *dbe; { MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data; @@ -304,15 +303,21 @@ BOOL CDbxMDBX::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei) return 1; dbe = (const DBEvent*)data.iov_base; - cbBlob = data.iov_len - sizeof(DBEvent); } dbei->szModule = GetModuleName(dbe->iModuleId); dbei->timestamp = dbe->timestamp; dbei->flags = dbe->flags; dbei->eventType = dbe->wEventType; - size_t bytesToCopy = min(dbei->cbBlob, cbBlob); - dbei->cbBlob = dbe->cbBlob; + + DWORD cbBlob = dbe->cbBlob; + size_t bytesToCopy = cbBlob; + if (dbei->cbBlob == -1) + dbei->pBlob = (PBYTE)mir_calloc(cbBlob + 2); + else if (dbei->cbBlob < cbBlob) + bytesToCopy = dbei->cbBlob; + + dbei->cbBlob = (DWORD)cbBlob; if (bytesToCopy && dbei->pBlob) { BYTE *pSrc = (BYTE*)dbe + sizeof(DBEvent); if (dbe->flags & DBEF_ENCRYPTED) { diff --git a/plugins/Dbx_sqlite/src/dbevents.cpp b/plugins/Dbx_sqlite/src/dbevents.cpp index cbfa67485a..6244928107 100755 --- a/plugins/Dbx_sqlite/src/dbevents.cpp +++ b/plugins/Dbx_sqlite/src/dbevents.cpp @@ -307,7 +307,12 @@ BOOL CDbxSQLite::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei) dbei->flags = sqlite3_column_int64(stmt, 3); DWORD cbBlob = sqlite3_column_int64(stmt, 4); - int bytesToCopy = (dbei->cbBlob < cbBlob) ? dbei->cbBlob : cbBlob; + size_t bytesToCopy = cbBlob; + if (dbei->cbBlob == -1) + dbei->pBlob = (PBYTE)mir_calloc(cbBlob + 2); + else if (dbei->cbBlob < cbBlob) + bytesToCopy = dbei->cbBlob; + dbei->cbBlob = cbBlob; if (bytesToCopy && dbei->pBlob) { BYTE *data = (BYTE *)sqlite3_column_blob(stmt, 5); diff --git a/plugins/HistoryLinkListPlus/src/linklist.cpp b/plugins/HistoryLinkListPlus/src/linklist.cpp index 4b0e97f785..016724e8fe 100644 --- a/plugins/HistoryLinkListPlus/src/linklist.cpp +++ b/plugins/HistoryLinkListPlus/src/linklist.cpp @@ -90,11 +90,9 @@ static INT_PTR LinkList_Main(WPARAM hContact, LPARAM) memset(listStart, 0, sizeof(LISTELEMENT)); do { - DBEVENTINFO dbe = {}; - dbe.cbBlob = db_event_getBlobSize(hEvent); - dbe.pBlob = (PBYTE)mir_alloc(dbe.cbBlob + 1); + DB::EventInfo dbe; + dbe.cbBlob = -1; db_event_get(hEvent, &dbe); - dbe.pBlob[dbe.cbBlob] = 0; if (dbe.eventType == EVENTTYPE_MESSAGE) { // Call function to find URIs @@ -109,7 +107,6 @@ static INT_PTR LinkList_Main(WPARAM hContact, LPARAM) actCount++; if (((int)(((float)actCount / histCount) * 100.00)) % 10 == 0) SendMessage(hWndProgress, WM_COMMAND, 100, ((int)(((float)actCount / histCount) * 100.00))); - mir_free(dbe.pBlob); } while (hEvent = pCursor.FetchNext()); diff --git a/plugins/HistoryLinkListPlus/src/linklist_fct.cpp b/plugins/HistoryLinkListPlus/src/linklist_fct.cpp index dcfbe5711f..63fbd15fa3 100644 --- a/plugins/HistoryLinkListPlus/src/linklist_fct.cpp +++ b/plugins/HistoryLinkListPlus/src/linklist_fct.cpp @@ -380,17 +380,13 @@ void WriteLinkList(HWND hDlg, BYTE params, LISTELEMENT *listStart, LPCTSTR searc // Perform deep scan if (actualElement->hEvent != NULL) { - DBEVENTINFO dbe = {}; - dbe.cbBlob = db_event_getBlobSize(actualElement->hEvent); - dbe.pBlob = (PBYTE)mir_alloc(dbe.cbBlob + 1); + DB::EventInfo dbe; + dbe.cbBlob = -1; db_event_get(actualElement->hEvent, &dbe); - dbe.pBlob[dbe.cbBlob] = 0; - LPTSTR msg = DbEvent_GetTextW(&dbe, CP_ACP); + + ptrW msg(DbEvent_GetTextW(&dbe, CP_ACP)); if (wcsstr(msg, searchString)) filter3 = 1; - - mir_free(dbe.pBlob); - mir_free(msg); } else filter3 = 0; } @@ -614,16 +610,12 @@ void WriteMessage(HWND hDlg, LISTELEMENT *listStart, int actLinePos) if (actualElement->linePos == actLinePos) { MEVENT hEvent = actualElement->hEvent; if (hEvent != NULL) { - DBEVENTINFO dbe = {}; - dbe.cbBlob = db_event_getBlobSize(hEvent); - dbe.pBlob = (PBYTE)mir_alloc(dbe.cbBlob + 1); + DB::EventInfo dbe; + dbe.cbBlob = -1; db_event_get(hEvent, &dbe); - dbe.pBlob[dbe.cbBlob] = 0; - LPCTSTR msg = DbEvent_GetTextW(&dbe, CP_ACP); - SetDlgItemText(hDlg, IDC_MESSAGE, nullptr); - SendDlgItemMessage(hDlg, IDC_MESSAGE, EM_REPLACESEL, FALSE, (LPARAM)msg); - mir_free((void*)msg); - mir_free(dbe.pBlob); + + SetDlgItemTextW(hDlg, IDC_MESSAGE, L""); + SendDlgItemMessage(hDlg, IDC_MESSAGE, EM_REPLACESEL, FALSE, ptrW(DbEvent_GetTextW(&dbe, CP_ACP))); } break; } @@ -771,18 +763,14 @@ void GetListInfo(BYTE params, LISTELEMENT *listStart, LPCTSTR searchString, size // Perform deep scan if (actualElement->hEvent != NULL) { - DBEVENTINFO dbe = {}; - dbe.cbBlob = db_event_getBlobSize(actualElement->hEvent); - dbe.pBlob = (PBYTE)mir_alloc(dbe.cbBlob + 1); + DB::EventInfo dbe; + dbe.cbBlob = -1; db_event_get(actualElement->hEvent, &dbe); - dbe.pBlob[dbe.cbBlob] = 0; + if (wcsstr((LPTSTR)dbe.pBlob, searchString)) filter3 = 1; - - mir_free(dbe.pBlob); } - else - filter3 = 0; + else filter3 = 0; } else { @@ -790,8 +778,7 @@ void GetListInfo(BYTE params, LISTELEMENT *listStart, LPCTSTR searchString, size filter3 = 1; } } - else - filter3 = 1; + else filter3 = 1; if ((filter1 == 1) && (filter2 == 1) && (filter3 == 1)) { @@ -1171,27 +1158,24 @@ This function is derived from his Wordlookup Plugin int DBUpdate(WPARAM wParam, LPARAM hEvent) { HWND hDlg = WindowList_Find(hWindowList, wParam); - DIALOGPARAM *DlgParam; HMENU listMenu = GetMenu(hDlg); - int linkNum = 0; - - DlgParam = (DIALOGPARAM *)GetWindowLongPtr(hDlg, GWLP_USERDATA); + DIALOGPARAM *DlgParam = (DIALOGPARAM *)GetWindowLongPtr(hDlg, GWLP_USERDATA); if (GetUpdateSetting() != 1) return 0; if (hDlg) { - DBEVENTINFO dbe = {}; - dbe.cbBlob = db_event_getBlobSize(hEvent); - dbe.pBlob = (PBYTE)mir_alloc((size_t)dbe.cbBlob + 1); - db_event_get(hEvent, &dbe); + DB::EventInfo dbe; + dbe.cbBlob = -1; + if (db_event_get(hEvent, &dbe)) + return 0; + if (dbe.eventType == EVENTTYPE_MESSAGE) { // Call function to find URIs - linkNum = ExtractURI(&dbe, hEvent, DlgParam->listStart); + int linkNum = ExtractURI(&dbe, hEvent, DlgParam->listStart); if (linkNum > 0) WriteLinkList(hDlg, GetFlags(listMenu), DlgParam->listStart, nullptr, linkNum); } - mir_free(dbe.pBlob); } return 0; } diff --git a/plugins/IEView/src/HTMLBuilder.cpp b/plugins/IEView/src/HTMLBuilder.cpp index a0ad5d7528..fcbd404f80 100644 --- a/plugins/IEView/src/HTMLBuilder.cpp +++ b/plugins/IEView/src/HTMLBuilder.cpp @@ -198,21 +198,19 @@ void HTMLBuilder::appendEventOld(IEView *view, IEVIEWEVENT *event) newEvent.hwnd = event->hwnd; for (int eventIdx = 0; hDbEvent != NULL && (eventIdx < event->count || event->count == -1); eventIdx++) { - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - if (dbei.cbBlob == 0xFFFFFFFF) { + DB::EventInfo dbei; + dbei.cbBlob = -1; + if (db_event_get(hDbEvent, &dbei)) { hDbEvent = db_event_next(event->hContact, hDbEvent); continue; } - dbei.pBlob = (PBYTE)malloc(dbei.cbBlob); - db_event_get(hDbEvent, &dbei); + if (!(dbei.flags & DBEF_SENT) && dbei.eventType == EVENTTYPE_MESSAGE) { db_event_markRead(event->hContact, hDbEvent); g_clistApi.pfnRemoveEvent(event->hContact, hDbEvent); } if (!isDbEventShown(&dbei)) { - free(dbei.pBlob); hDbEvent = db_event_next(event->hContact, hDbEvent); continue; } @@ -265,7 +263,7 @@ void HTMLBuilder::appendEventOld(IEView *view, IEVIEWEVENT *event) eventData->szText.w = DbEvent_GetTextW(&dbei, newEvent.codepage); eventData->iType = IEED_EVENT_MESSAGE; } - free(dbei.pBlob); + eventData->next = nullptr; if (prevEventData != nullptr) prevEventData->next = eventData; diff --git a/plugins/Import/src/dbrw/dbevents.cpp b/plugins/Import/src/dbrw/dbevents.cpp index ceb1a9a8f4..4fcfa650fe 100644 --- a/plugins/Import/src/dbrw/dbevents.cpp +++ b/plugins/Import/src/dbrw/dbevents.cpp @@ -78,17 +78,23 @@ STDMETHODIMP_(BOOL) CDbxSQLite::GetEvent(MEVENT hDbEvent, DBEVENTINFO *dbei) sqlite3_stmt *stmt = evt_stmts_prep[SQL_EVT_STMT_GET]; sqlite3_bind_int(stmt, 1, hDbEvent); if (sql_step(stmt) == SQLITE_ROW) { - unsigned copySize; const void *blob = sqlite3_column_blob(stmt, 4); - const unsigned size = sqlite3_column_int(stmt, 5); dbei->timestamp = (DWORD)sqlite3_column_int(stmt, 1); dbei->flags = (DWORD)sqlite3_column_int(stmt, 2); dbei->eventType = (WORD)sqlite3_column_int(stmt, 3); dbei->szModule = mir_strdup((char*)sqlite3_column_text(stmt, 7)); - copySize = sizecbBlob ? size : dbei->cbBlob; - memcpy(dbei->pBlob, blob, copySize); - dbei->cbBlob = copySize; + + DWORD cbBlob = sqlite3_column_int(stmt, 5); + size_t bytesToCopy = cbBlob; + if (dbei->cbBlob == -1) + dbei->pBlob = (PBYTE)mir_calloc(cbBlob + 2); + else if (dbei->cbBlob < cbBlob) + bytesToCopy = dbei->cbBlob; + + dbei->cbBlob = cbBlob; + if (bytesToCopy && dbei->pBlob) + memcpy(dbei->pBlob, blob, bytesToCopy); res = 0; } sql_reset(stmt); diff --git a/plugins/MirLua/src/Modules/m_database.cpp b/plugins/MirLua/src/Modules/m_database.cpp index f00887a7da..538f7682a3 100644 --- a/plugins/MirLua/src/Modules/m_database.cpp +++ b/plugins/MirLua/src/Modules/m_database.cpp @@ -636,11 +636,9 @@ DBEVENTINFO* MT::Init(lua_State *L) { MEVENT hDbEvent = luaL_checkinteger(L, 1); - DBEVENTINFO* dbei = (DBEVENTINFO*)mir_calloc(sizeof(DBEVENTINFO)); - dbei->cbBlob = db_event_getBlobSize((MEVENT)hDbEvent); - dbei->pBlob = (PBYTE)mir_calloc(dbei->cbBlob); + DBEVENTINFO *dbei = (DBEVENTINFO *)mir_calloc(sizeof(DBEVENTINFO)); + dbei->cbBlob = -1; db_event_get((MEVENT)hDbEvent, dbei); - return dbei; } diff --git a/plugins/MirandaG15/src/CAppletManager.cpp b/plugins/MirandaG15/src/CAppletManager.cpp index 24d0c75abe..00ed5f39ac 100644 --- a/plugins/MirandaG15/src/CAppletManager.cpp +++ b/plugins/MirandaG15/src/CAppletManager.cpp @@ -793,17 +793,11 @@ void CAppletManager::MarkMessageAsRead(MCONTACT hContact, MEVENT hEvent) bool CAppletManager::TranslateDBEvent(CEvent *pEvent, WPARAM hContact, LPARAM hdbevent) { // Create struct for dbevent - DBEVENTINFO dbevent = {}; - dbevent.cbBlob = db_event_getBlobSize(hdbevent); - if (dbevent.cbBlob == -1) // hdbevent is invalid + DB::EventInfo dbevent; + dbevent.cbBlob = -1; + if (db_event_get(hdbevent, &dbevent) != 0) return false; - dbevent.pBlob = (PBYTE)malloc(dbevent.cbBlob); - if (db_event_get(hdbevent, &dbevent) != 0) { - free(dbevent.pBlob); - return false; - } - pEvent->dwFlags = dbevent.flags; pEvent->hContact = hContact; pEvent->hValue = hdbevent; @@ -811,17 +805,10 @@ bool CAppletManager::TranslateDBEvent(CEvent *pEvent, WPARAM hContact, LPARAM hd time_t timestamp = (time_t)dbevent.timestamp; localtime_s(&pEvent->Time, ×tamp); pEvent->bTime = true; - /* - if(dbevent.eventType == EVENTTYPE_MESSAGE && dbevent.flags & DBEF_READ) { - free(dbevent.pBlob); - return false; - } - */ + // Skip events from the user except for messages - if (dbevent.eventType != EVENTTYPE_MESSAGE && (dbevent.flags & DBEF_SENT)) { - free(dbevent.pBlob); + if (dbevent.eventType != EVENTTYPE_MESSAGE && (dbevent.flags & DBEF_SENT)) return false; - } int msglen = 0; @@ -877,9 +864,9 @@ bool CAppletManager::TranslateDBEvent(CEvent *pEvent, WPARAM hContact, LPARAM hd pEvent->strDescription = TranslateString(L"Incoming file from %s", strName.c_str()); pEvent->eType = EVENT_FILE; break; + default: return false; - break; } if (CConfig::GetBoolSetting(NOTIFY_SHOWPROTO)) { @@ -887,8 +874,6 @@ bool CAppletManager::TranslateDBEvent(CEvent *pEvent, WPARAM hContact, LPARAM hd pEvent->strDescription = L"(" + toTstring(szProto) + L") " + pEvent->strDescription; } - // Clean up - free(dbevent.pBlob); return true; } diff --git a/plugins/Msg_Export/src/utils.cpp b/plugins/Msg_Export/src/utils.cpp index d629ff16f1..e5cba7f1f7 100755 --- a/plugins/Msg_Export/src/utils.cpp +++ b/plugins/Msg_Export/src/utils.cpp @@ -961,18 +961,10 @@ int nExportEvent(WPARAM hContact, LPARAM hDbEvent) bool bExportEvent(MCONTACT hContact, MEVENT hDbEvent, HANDLE hFile, const wstring &sFilePath, bool bAppendOnly) { - DBEVENTINFO dbei = {}; - int nSize = db_event_getBlobSize(hDbEvent); - if (nSize > 0) { - dbei.cbBlob = nSize; - dbei.pBlob = (PBYTE)malloc(dbei.cbBlob + 2); - dbei.pBlob[dbei.cbBlob] = 0; - dbei.pBlob[dbei.cbBlob + 1] = 0; - // Double null terminate, this should prevent most errors - // where the blob received has an invalid format - } - bool result = true; + + DB::EventInfo dbei; + dbei.cbBlob = -1; if (!db_event_get(hDbEvent, &dbei)) { if (db_mc_isMeta(hContact)) hContact = db_event_getContact(hDbEvent); @@ -980,8 +972,7 @@ bool bExportEvent(MCONTACT hContact, MEVENT hDbEvent, HANDLE hFile, const wstrin // Write the event result = ExportDBEventInfo(hContact, hFile, sFilePath, dbei, bAppendOnly); } - if (dbei.pBlob) - free(dbei.pBlob); + return result; } diff --git a/plugins/NewEventNotify/src/popup.cpp b/plugins/NewEventNotify/src/popup.cpp index 07a074042e..b2bd6190f0 100644 --- a/plugins/NewEventNotify/src/popup.cpp +++ b/plugins/NewEventNotify/src/popup.cpp @@ -358,12 +358,10 @@ int PopupShow(MCONTACT hContact, MEVENT hEvent, UINT eventType) } // get DBEVENTINFO with pBlob if preview is needed (when is test then is off) - DBEVENTINFO dbe = {}; + DB::EventInfo dbe; if (hEvent) { - if ((g_plugin.bPreview || eventType == EVENTTYPE_ADDED || eventType == EVENTTYPE_AUTHREQUEST)) { - dbe.cbBlob = db_event_getBlobSize(hEvent); - dbe.pBlob = (PBYTE)mir_alloc(dbe.cbBlob); - } + if ((g_plugin.bPreview || eventType == EVENTTYPE_ADDED || eventType == EVENTTYPE_AUTHREQUEST)) + dbe.cbBlob = -1; db_event_get(hEvent, &dbe); } @@ -412,8 +410,6 @@ int PopupShow(MCONTACT hContact, MEVENT hEvent, UINT eventType) mir_free(pdata); } - if (dbe.pBlob) - mir_free(dbe.pBlob); return 0; } @@ -458,16 +454,12 @@ int PopupUpdate(MCONTACT hContact, MEVENT hEvent) iEvent++; // get DBEVENTINFO with pBlob if preview is needed (when is test then is off) - DBEVENTINFO dbe = {}; - dbe.pBlob = nullptr; - dbe.cbBlob = 0; - if (g_plugin.bPreview && eventData->hEvent) { - dbe.cbBlob = db_event_getBlobSize(eventData->hEvent); - dbe.pBlob = (PBYTE)mir_alloc(dbe.cbBlob); - } - - if (eventData->hEvent) + DB::EventInfo dbe; + if (eventData->hEvent) { + if (g_plugin.bPreview) + dbe.cbBlob = -1; db_event_get(eventData->hEvent, &dbe); + } if (g_plugin.bShowDate || g_plugin.bShowTime) { wchar_t timestamp[MAX_DATASIZE]; @@ -486,8 +478,6 @@ int PopupUpdate(MCONTACT hContact, MEVENT hEvent) mir_snwprintf(lpzText, L"%s%s", lpzText, szEventPreview); mir_free(szEventPreview); - if (dbe.pBlob) - mir_free(dbe.pBlob); if (doReverse) { if ((iEvent >= g_plugin.iNumberMsg && g_plugin.iNumberMsg) || !eventData->next) break; diff --git a/plugins/NewStory/src/history.cpp b/plugins/NewStory/src/history.cpp index f0f3fae9a4..d69706efad 100644 --- a/plugins/NewStory/src/history.cpp +++ b/plugins/NewStory/src/history.cpp @@ -709,17 +709,8 @@ public: bool bAppendOnly = false; DB::ECPTR pCursor(DB::Events(m_hContact)); while (MEVENT hDbEvent = pCursor.FetchNext()) { - DBEVENTINFO dbei = {}; - int nSize = db_event_getBlobSize(hDbEvent); - if (nSize > 0) { - dbei.cbBlob = nSize; - dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob + 2); - dbei.pBlob[dbei.cbBlob] = 0; - dbei.pBlob[dbei.cbBlob + 1] = 0; - // Double null terminate, this should prevent most errors - // where the blob received has an invalid format - } - + DB::EventInfo dbei; + dbei.cbBlob = -1; if (!db_event_get(hDbEvent, &dbei)) { if (bAppendOnly) { SetFilePointer(hFile, -3, nullptr, FILE_END); @@ -753,8 +744,6 @@ public: output += "\n]}"; WriteFile(hFile, output.c_str(), (int)output.size(), &dwBytesWritten, nullptr); - if (dbei.pBlob) - mir_free(dbei.pBlob); } bAppendOnly = true; diff --git a/plugins/Scriver/src/msgdialog.cpp b/plugins/Scriver/src/msgdialog.cpp index c8b0f89f2e..b9a57e3f00 100644 --- a/plugins/Scriver/src/msgdialog.cpp +++ b/plugins/Scriver/src/msgdialog.cpp @@ -484,7 +484,7 @@ void CMsgDialog::onClick_Quote(CCtrlButton*) if (m_hDbEventLast == 0) return; - SETTEXTEX st; + SETTEXTEX st; st.flags = ST_SELECTION; st.codepage = 1200; @@ -495,12 +495,11 @@ void CMsgDialog::onClick_Quote(CCtrlButton*) mir_free(buffer); } else { - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(m_hDbEventLast); - if (dbei.cbBlob == 0xFFFFFFFF) + DB::EventInfo dbei; + dbei.cbBlob = -1; + if (db_event_get(m_hDbEventLast, &dbei)) return; - dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob); - db_event_get(m_hDbEventLast, &dbei); + if (DbEventIsMessageOrCustom(&dbei)) { buffer = DbEvent_GetTextW(&dbei, CP_ACP); if (buffer != nullptr) { @@ -509,7 +508,6 @@ void CMsgDialog::onClick_Quote(CCtrlButton*) mir_free(buffer); } } - mir_free(dbei.pBlob); } SetFocus(m_message.GetHwnd()); } diff --git a/plugins/Scriver/src/msglog.cpp b/plugins/Scriver/src/msglog.cpp index 062657a3ac..f0914bd2cd 100644 --- a/plugins/Scriver/src/msglog.cpp +++ b/plugins/Scriver/src/msglog.cpp @@ -78,17 +78,13 @@ int DbEventIsShown(DBEVENTINFO &dbei) EventData* CMsgDialog::GetEventFromDB(MCONTACT hContact, MEVENT hDbEvent) { - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - if (dbei.cbBlob == -1) + DB::EventInfo dbei; + dbei.cbBlob = -1; + if (db_event_get(hDbEvent, &dbei)) return nullptr; - dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob); - db_event_get(hDbEvent, &dbei); - if (!DbEventIsShown(dbei)) { - mir_free(dbei.pBlob); + if (!DbEventIsShown(dbei)) return nullptr; - } EventData *evt = (EventData*)mir_calloc(sizeof(EventData)); evt->custom = DbEventIsCustomForMsgWindow(&dbei); @@ -125,7 +121,6 @@ EventData* CMsgDialog::GetEventFromDB(MCONTACT hContact, MEVENT hDbEvent) if (!m_bUseRtl && Utils_IsRtl(evt->szText.w)) evt->dwFlags |= IEEDF_RTL; - mir_free(dbei.pBlob); return evt; } diff --git a/plugins/StopSpamMod/src/stopspam.cpp b/plugins/StopSpamMod/src/stopspam.cpp index b32afb3e90..f1e7ca9361 100755 --- a/plugins/StopSpamMod/src/stopspam.cpp +++ b/plugins/StopSpamMod/src/stopspam.cpp @@ -19,14 +19,11 @@ int OnDbEventAdded(WPARAM hContact, LPARAM hDbEvent) { - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - if (dbei.cbBlob == -1) + DB::EventInfo dbei; + dbei.cbBlob = -1; + if (db_event_get(hDbEvent, &dbei)) return 0; - dbei.pBlob = (BYTE*)alloca(dbei.cbBlob); - db_event_get(hDbEvent, &dbei); - // if event is in protocol that is not despammed if (!ProtoInList(dbei.szModule)) return 0; diff --git a/plugins/StopSpamPlus/src/events.cpp b/plugins/StopSpamPlus/src/events.cpp index ae3c4794a6..e6f948f2ef 100644 --- a/plugins/StopSpamPlus/src/events.cpp +++ b/plugins/StopSpamPlus/src/events.cpp @@ -4,15 +4,11 @@ int OnDbEventAdded(WPARAM, LPARAM lParam) { MEVENT hDbEvent = (MEVENT)lParam; - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - if (-1 == dbei.cbBlob) + DB::EventInfo dbei; + dbei.cbBlob = -1; + if (db_event_get(hDbEvent, &dbei)) return 0; - mir_ptr blob((LPBYTE)mir_alloc(dbei.cbBlob)); - dbei.pBlob = blob; - db_event_get(hDbEvent, &dbei); - // if event is in protocol that is not despammed if (g_sets.ProtoDisabled(dbei.szModule)) return 0; diff --git a/plugins/TabSRMM/src/eventpopups.cpp b/plugins/TabSRMM/src/eventpopups.cpp index fd2538208a..c69982526f 100644 --- a/plugins/TabSRMM/src/eventpopups.cpp +++ b/plugins/TabSRMM/src/eventpopups.cpp @@ -313,11 +313,9 @@ static int PopupUpdateT(MCONTACT hContact, MEVENT hEvent) else szHeader[0] = 0; - DBEVENTINFO dbe = {}; - if (pdata->pluginOptions->bPreview && hContact) { - dbe.cbBlob = db_event_getBlobSize(hEvent); - dbe.pBlob = (PBYTE)mir_alloc(dbe.cbBlob); - } + DB::EventInfo dbe; + if (pdata->pluginOptions->bPreview && hContact) + dbe.cbBlob = -1; db_event_get(hEvent, &dbe); wchar_t timestamp[MAX_DATASIZE]; @@ -360,8 +358,6 @@ static int PopupUpdateT(MCONTACT hContact, MEVENT hEvent) pdata->nrEventsAlloced += 5; pdata->eventData = (EVENT_DATAT *)mir_realloc(pdata->eventData, pdata->nrEventsAlloced * sizeof(EVENT_DATAT)); } - if (dbe.pBlob) - mir_free(dbe.pBlob); PUChangeTextW(pdata->hWnd, lpzText); return 0; @@ -373,12 +369,10 @@ static int PopupShowT(NEN_OPTIONS *pluginOptions, MCONTACT hContact, MEVENT hEve if (arPopupList.getCount() >= MAX_POPUPS) return 2; - DBEVENTINFO dbe = {}; + DB::EventInfo dbe; // fix for a crash - if (hEvent && (pluginOptions->bPreview || hContact == 0)) { - dbe.cbBlob = db_event_getBlobSize(hEvent); - dbe.pBlob = (PBYTE)mir_alloc(dbe.cbBlob); - } + if (hEvent && (pluginOptions->bPreview || hContact == 0)) + dbe.cbBlob = -1; db_event_get(hEvent, &dbe); if (hEvent == 0 && hContact == 0) @@ -437,10 +431,6 @@ static int PopupShowT(NEN_OPTIONS *pluginOptions, MCONTACT hContact, MEVENT hEve pdata->nrMerged = 1; PUAddPopupW(&pud); arPopupList.insert(pdata); - - if (dbe.pBlob) - mir_free(dbe.pBlob); - return 0; } diff --git a/plugins/TabSRMM/src/msglog.cpp b/plugins/TabSRMM/src/msglog.cpp index 49d03fa2bd..1d9a5d56c2 100644 --- a/plugins/TabSRMM/src/msglog.cpp +++ b/plugins/TabSRMM/src/msglog.cpp @@ -437,11 +437,7 @@ static char* Template_CreateRTFFromDbEvent(CMsgDialog *dat, MCONTACT hContact, M if (streamData->dbei != nullptr) dbei = *(streamData->dbei); else { - dbei.cbBlob = db_event_getBlobSize(hDbEvent); - if (dbei.cbBlob == -1) - return nullptr; - - dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob); + dbei.cbBlob = -1; db_event_get(hDbEvent, &dbei); if (!DbEventIsShown(&dbei)) { mir_free(dbei.pBlob); diff --git a/plugins/Variables/src/parse_miranda.cpp b/plugins/Variables/src/parse_miranda.cpp index b2687e5490..452989048d 100644 --- a/plugins/Variables/src/parse_miranda.cpp +++ b/plugins/Variables/src/parse_miranda.cpp @@ -564,17 +564,12 @@ static wchar_t* parseDbEvent(ARGUMENTSINFO *ai) if (hDbEvent == NULL) return nullptr; - DBEVENTINFO dbe = {}; - dbe.cbBlob = db_event_getBlobSize(hDbEvent); - dbe.pBlob = (PBYTE)mir_calloc(dbe.cbBlob); - if (db_event_get(hDbEvent, &dbe)) { - mir_free(dbe.pBlob); + DB::EventInfo dbe; + dbe.cbBlob = -1; + if (db_event_get(hDbEvent, &dbe)) return nullptr; - } - wchar_t *res = DbEvent_GetTextW(&dbe, CP_ACP); - mir_free(dbe.pBlob); - return res; + return DbEvent_GetTextW(&dbe, CP_ACP); } static wchar_t* parseTranslate(ARGUMENTSINFO *ai) diff --git a/plugins/YARelay/src/main.cpp b/plugins/YARelay/src/main.cpp index 4a29fedc9f..5abc1cbc44 100644 --- a/plugins/YARelay/src/main.cpp +++ b/plugins/YARelay/src/main.cpp @@ -113,12 +113,8 @@ static int MessageEventAdded(WPARAM hContact, LPARAM hDBEvent) return 0; // receive message from DB - DBEVENTINFO dbei = {}; - dbei.cbBlob = db_event_getBlobSize(hDBEvent); - if (dbei.cbBlob == -1) - return 0; - - dbei.pBlob = (unsigned char*)alloca(dbei.cbBlob); + DB::EventInfo dbei; + dbei.cbBlob = -1; db_event_get(hDBEvent, &dbei); if (dbei.flags & DBEF_SENT || dbei.flags & DBEF_READ || (dbei.eventType != EVENTTYPE_MESSAGE)) return 0; diff --git a/plugins/wbOSD/src/events.cpp b/plugins/wbOSD/src/events.cpp index 81429c9b09..e82fd92025 100644 --- a/plugins/wbOSD/src/events.cpp +++ b/plugins/wbOSD/src/events.cpp @@ -129,12 +129,8 @@ int ContactStatusChanged(WPARAM wParam, LPARAM lParam) int HookedNewEvent(WPARAM wParam, LPARAM hDBEvent) { logmsg("HookedNewEvent1"); - DBEVENTINFO dbe; - dbe.cbBlob = db_event_getBlobSize(hDBEvent); - if (dbe.cbBlob == -1) - return 0; - - dbe.pBlob = (PBYTE)malloc(dbe.cbBlob); + DB::EventInfo dbe; + dbe.cbBlob = -1; if (db_event_get(hDBEvent, &dbe)) return 0; @@ -182,7 +178,7 @@ int HookedNewEvent(WPARAM wParam, LPARAM hDBEvent) pbuf++; } - wchar_t *c1 = nullptr, *c2 = nullptr; + ptrW c1, c2; if (i1 == 1) c1 = mir_wstrdup(Clist_GetContactDisplayName(wParam)); else if (i1 == 2) @@ -196,8 +192,5 @@ int HookedNewEvent(WPARAM wParam, LPARAM hDBEvent) wchar_t buffer[512]; mir_snwprintf(buffer, buf, c1, c2); ShowOSD(buffer, 0, g_plugin.getDword("clr_msg", DEFAULT_CLRMSG), wParam); - - mir_free(c1); - mir_free(c2); return 0; } -- cgit v1.2.3