From 5d6a4181191ec5eb92f2596fe6099f4009ec12ff Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Sun, 29 Jul 2012 22:17:00 +0300 Subject: fixed memorry corruption's improoved autoexchange --- src/gpg_wrapper.cpp | 4 +-- src/init.cpp | 2 +- src/main.cpp | 80 ++++++++++++++++++++++++++-------------------- src/messages.cpp | 91 +++++++++++++++++++++++++++++++---------------------- src/options.cpp | 21 +++++++------ src/utilities.cpp | 25 ++++++++------- 6 files changed, 126 insertions(+), 97 deletions(-) (limited to 'src') diff --git a/src/gpg_wrapper.cpp b/src/gpg_wrapper.cpp index 410a0d5..38f8688 100755 --- a/src/gpg_wrapper.cpp +++ b/src/gpg_wrapper.cpp @@ -96,8 +96,8 @@ pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD commandline += _T("--display-charset utf-8 "); commandline += _T("-z 9 "); commandline += *acommandline; - delete [] bin_path; //hmm - delete [] home_dir; + mir_free(bin_path); + mir_free(home_dir); } debuglog<szProtoName); + strcat(svc, PS_ICQ_ADDCAPABILITY); + if(ServiceExists(svc)) + CallService(svc, 0, (LPARAM)&cap); + } } } diff --git a/src/messages.cpp b/src/messages.cpp index f2e3c35..ba9a36c 100755 --- a/src/messages.cpp +++ b/src/messages.cpp @@ -456,7 +456,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) s2 = output.find("<", s); else if(s2 > output.find("<", s)) s2 = output.find("<", s); - tmp = new char [output.substr(s,s2-s-1).length()+1]; + tmp = (char*)mir_alloc(output.substr(s,s2-s-1).length()+1); strcpy(tmp, output.substr(s,s2-s-1).c_str()); mir_utf8decode(tmp, 0); DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyMainName", tmp); @@ -468,14 +468,14 @@ int RecvMsgSvc(WPARAM w, LPARAM l) s2++; if(output[s] == ')') { - tmp = new char [output.substr(s2,s-s2).length()+1]; + tmp = (char*)mir_alloc(output.substr(s2,s-s2).length()+1); strcpy(tmp, output.substr(s2,s-s2).c_str()); mir_utf8decode(tmp, 0); DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyComment", tmp); mir_free(tmp); s+=3; s2 = output.find(">", s); - tmp = new char [output.substr(s,s2-s).length()+1]; + tmp = (char*)mir_alloc(output.substr(s,s2-s).length()+1); strcpy(tmp, output.substr(s,s2-s).c_str()); mir_utf8decode(tmp, 0); DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyMainEmail", tmp); @@ -483,7 +483,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } else { - tmp = new char [output.substr(s2,s-s2).length()+1]; + tmp = (char*)mir_alloc(output.substr(s2,s-s2).length()+1); strcpy(tmp, output.substr(s2,s-s2).c_str()); mir_utf8decode(tmp, 0); DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyMainEmail", output.substr(s2,s-s2).c_str()); @@ -561,54 +561,64 @@ int RecvMsgSvc(WPARAM w, LPARAM l) DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 1); } mir_free(tmp); - if(!isContactHaveKey(ccs->hContact) && bAutoExchange && gpg_valid && gpg_keyexist) + return returnNoError(ccs->hContact); + } + else if(!isContactHaveKey(ccs->hContact) && bAutoExchange && gpg_valid && gpg_keyexist) + { + LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); + DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0); + if(uin) { - LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); - DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0); - if(uin) + char *proto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); + char svc[64]; + strcpy(svc, proto); + strcat(svc, PS_ICQ_CHECKCAPABILITY); + if(ServiceExists(svc)) { - if(ServiceExists("ICQ"PS_ICQ_CHECKCAPABILITY)) + ICQ_CUSTOMCAP cap = {0}; + strcpy(cap.caps, "GPG AutoExchange"); + if(CallService(svc, (WPARAM)ccs->hContact, (LPARAM)&cap)) { - ICQ_CUSTOMCAP cap = {0}; - strcpy(cap.caps, "GPG AutoExchange"); - if(CallService("ICQ"PS_ICQ_CHECKCAPABILITY, (WPARAM)ccs->hContact, (LPARAM)&cap)) - CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)"-----PGP KEY REQUEST-----"); + CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)"-----PGP KEY REQUEST-----"); + return returnNoError(ccs->hContact); } } - else + } + else + { + TCHAR *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", _T("")); + if(jid[0]) { - TCHAR *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", _T("")); - if(jid[0]) + extern list Accounts; + list::iterator end = Accounts.end(); + for(list::iterator p = Accounts.begin(); p != end; p++) { - extern list Accounts; - list::iterator end = Accounts.end(); - for(list::iterator p = Accounts.begin(); p != end; p++) + TCHAR *caps = (*p)->getJabberInterface()->Net()->GetResourceFeatures(jid); + if(caps) { - TCHAR *caps = (*p)->getJabberInterface()->Net()->GetResourceFeatures(jid); - if(caps) + wstring str; + for(int i =0;;i++) + { + str.push_back(caps[i]); + if(caps[i] == '\0') + if(caps[i+1] == '\0') + break; + } + mir_free(caps); + if(str.find(_T("GPG_Key_Auto_Exchange:0")) != string::npos) { - wstring str; - for(int i =0;;i++) - { - str.push_back(caps[i]); - if(caps[i] == '\0') - if(caps[i+1] == '\0') - break; - } - mir_free(caps); - if(str.find(_T("GPG_Key_Auto_Exchange:0")) != string::npos) - CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)0, (LPARAM)"-----PGP KEY REQUEST-----"); + CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)0, (LPARAM)"-----PGP KEY REQUEST-----"); + return returnNoError(ccs->hContact); } } } } } - return 1; } if(!(strstr(msg, "-----BEGIN PGP MESSAGE-----") && strstr(msg, "-----END PGP MESSAGE-----"))) return CallService(MS_PROTO_CHAINRECV, w, l); - boost::thread *thr = new boost::thread(boost::bind(RecvMsgSvc_func, ccs->hContact, str, msg, ccs->wParam, pre->timestamp)); - return returnNoError(ccs->hContact); + boost::thread *thr = new boost::thread(boost::bind(RecvMsgSvc_func, ccs->hContact, str, msg, ccs->wParam, pre->timestamp)); + return returnNoError(ccs->hContact); } int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) @@ -827,11 +837,16 @@ int SendMsgSvc(WPARAM w, LPARAM l) DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0); if(uin) { - if(ServiceExists("ICQ"PS_ICQ_CHECKCAPABILITY)) + char *proto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); + char svc[64]; + strcpy(svc, proto); + strcat(svc, PS_ICQ_CHECKCAPABILITY); + + if(ServiceExists(svc)) { ICQ_CUSTOMCAP cap = {0}; strcpy(cap.caps, "GPG AutoExchange"); - if(CallService("ICQ"PS_ICQ_CHECKCAPABILITY, (WPARAM)ccs->hContact, (LPARAM)&cap)) + if(CallService(svc, (WPARAM)ccs->hContact, (LPARAM)&cap)) { CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)ccs->wParam, (LPARAM)"-----PGP KEY REQUEST-----"); hcontact_data[ccs->hContact].msgs_to_send.push_back(msg); @@ -854,7 +869,7 @@ int SendMsgSvc(WPARAM w, LPARAM l) if(caps) { wstring str; - for(int i =0;;i++) + for(int i=0;;i++) { str.push_back(caps[i]); if(caps[i] == '\0') diff --git a/src/options.cpp b/src/options.cpp index a990003..f03879c 100755 --- a/src/options.cpp +++ b/src/options.cpp @@ -796,7 +796,7 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP { tmp = UniGetContactSettingUtf(hcnt, szGPGModuleName, "GPGPubKey", _T("")); wstring str = tmp; - mir_free(tmp); + mir_free(tmp); tmp = NULL; wstring::size_type p = 0, stop = 0; if(!str.empty()) { @@ -878,7 +878,8 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP } mir_free(tmp2); } - mir_free(tmp); + if(tmp) + mir_free(tmp); SetDlgItemText(hwndDlg, IDC_PUBLIC_KEY_EDIT, !str.empty()?str.c_str():_T("")); } hPubKeyEdit = GetDlgItem(hwndDlg, IDC_PUBLIC_KEY_EDIT); @@ -904,16 +905,16 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP ws1 = 0; if(((ws2 = key_buf.find(_T("-----END PGP PUBLIC KEY BLOCK-----"))) != wstring::npos) && ((ws1 = key_buf.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----"))) != wstring::npos)) { - begin = new TCHAR [_tcslen(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")) + 1]; + begin = (TCHAR*)mir_alloc(sizeof(TCHAR) * (_tcslen(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")) + 1)); _tcscpy(begin, _T("-----BEGIN PGP PUBLIC KEY BLOCK-----")); - end = new TCHAR [_tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")) + 1]; + end = (TCHAR*)mir_alloc(sizeof( TCHAR) * (_tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")) + 1)); _tcscpy(end, _T("-----END PGP PUBLIC KEY BLOCK-----")); } else if(((ws2 = key_buf.find(_T("-----END PGP PRIVATE KEY BLOCK-----"))) != wstring::npos) && ((ws1 = key_buf.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----"))) != wstring::npos)) { - begin = new TCHAR [_tcslen(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")) + 1]; + begin = (TCHAR*)mir_alloc(sizeof(TCHAR) * (_tcslen(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")) + 1)); _tcscpy(begin, _T("-----BEGIN PGP PRIVATE KEY BLOCK-----")); - end = new TCHAR [_tcslen(_T("-----END PGP PRIVATE KEY BLOCK-----")) + 1]; + end = (TCHAR*)mir_alloc(sizeof(TCHAR) * (_tcslen(_T("-----END PGP PRIVATE KEY BLOCK-----")) + 1)); _tcscpy(end, _T("-----END PGP PRIVATE KEY BLOCK-----")); } else @@ -944,7 +945,7 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP else DBWriteContactSettingTString(hContact, szGPGModuleName, "GPGPubKey", key_buf.substr(ws1,ws2-ws1).c_str()); } - tmp = new TCHAR [key_buf.length()+1]; + tmp = (TCHAR*)mir_alloc(sizeof( TCHAR) * (key_buf.length()+1)); _tcscpy(tmp, key_buf.substr(ws1,ws2-ws1).c_str()); { //gpg execute block wstring cmd; @@ -1030,7 +1031,7 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP char *tmp2; string::size_type s = output.find("gpg: key ") + strlen("gpg: key "); string::size_type s2 = output.find(":", s); - tmp2 = new char [output.substr(s,s2-s).length()+1]; + tmp2 = (char*)mir_alloc(output.substr(s,s2-s).length()+1); strcpy(tmp2, output.substr(s,s2-s).c_str()); mir_utf8decode(tmp2, 0); { @@ -1072,7 +1073,7 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP s2 = output.find("<", s); if(s2 != string::npos) { - tmp2 = new char [output.substr(s,s2-s-1).length()+1]; + tmp2 = (char*)mir_alloc(output.substr(s,s2-s-1).length()+1); strcpy(tmp2, output.substr(s,s2-s-1).c_str()); mir_utf8decode(tmp2, 0); if(hContact) @@ -1167,7 +1168,7 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP } else { - tmp2 = new char [output.substr(s2,s-s2).length()+1]; + tmp2 = (char*)mir_alloc(output.substr(s2,s-s2).length()+1); strcpy(tmp2, output.substr(s2,s-s2).c_str()); mir_utf8decode(tmp2, 0); if(hContact) diff --git a/src/utilities.cpp b/src/utilities.cpp index 2207213..242ccc4 100755 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -23,9 +23,9 @@ TCHAR* __stdcall UniGetContactSettingUtf(HANDLE hContact, const char *szModule,c DBVARIANT dbv = {DBVT_DELETED}; TCHAR* szRes; if (DBGetContactSettingTString(hContact, szModule, szSetting, &dbv)) - return _tcsdup(szDef); + return mir_tstrdup(szDef); if(dbv.pszVal) - szRes = _tcsdup(dbv.ptszVal); + szRes = mir_tstrdup(dbv.ptszVal); DBFreeVariant(&dbv); return szRes; } @@ -35,9 +35,9 @@ char* __stdcall UniGetContactSettingUtf(HANDLE hContact, const char *szModule,co DBVARIANT dbv = {DBVT_DELETED}; char* szRes; if (DBGetContactSettingString(hContact, szModule, szSetting, &dbv)) - return _strdup(szDef); + return mir_strdup(szDef); if(dbv.pszVal) - szRes = _strdup(dbv.pszVal); + szRes = mir_strdup(dbv.pszVal); DBFreeVariant(&dbv); return szRes; } @@ -1089,14 +1089,14 @@ bool isGPGValid() else { mir_free(tmp); - TCHAR *path = new TCHAR [MAX_PATH]; - char *mir_path = new char [MAX_PATH]; + TCHAR *path = (TCHAR*)mir_alloc(sizeof(TCHAR)*MAX_PATH); + char *mir_path = (char*)mir_alloc(MAX_PATH); CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); SetCurrentDirectoryA(mir_path); tmp = mir_a2t(mir_path); mir_free(mir_path); - mir_realloc(path, (_tcslen(path)+128)*sizeof(TCHAR)); - TCHAR *gpg_path = new TCHAR [MAX_PATH]; + //mir_realloc(path, (_tcslen(path)+64)*sizeof(TCHAR)); + TCHAR *gpg_path = (TCHAR*)mir_alloc(sizeof(TCHAR)*MAX_PATH); _tcscpy(gpg_path, tmp); _tcscat(gpg_path, _T("\\GnuPG\\gpg.exe")); mir_free(tmp); @@ -1106,8 +1106,8 @@ bool isGPGValid() _tcscpy(path, _T("GnuPG\\gpg.exe")); } mir_free(gpg_path); - tmp = mir_wstrdup(path); - delete [] path; + tmp = mir_tstrdup(path); + mir_free(path); } DWORD len = MAX_PATH; if(gpg_exists) @@ -1137,14 +1137,15 @@ bool isGPGValid() if(p1 == string::npos) is_valid = false; } - mir_free(tmp); + mir_free(tmp); tmp = NULL; if(!gpg_exists) { wstring path_ = _wgetenv(_T("APPDATA")); path_ += _T("\\GnuPG"); tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", (TCHAR*)path_.c_str()); } - mir_free(tmp); + if(tmp) + mir_free(tmp); return is_valid; } -- cgit v1.2.3