From 05d6c1473c320e1f6f5c489983f64085d26504f5 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 15 Jan 2018 15:30:14 +0300 Subject: fix for various memory problems in SpellChecker --- plugins/SpellChecker/src/ardialog.cpp | 11 ++-- plugins/SpellChecker/src/autoreplace.cpp | 11 ++-- plugins/SpellChecker/src/stdafx.h | 6 +- plugins/SpellChecker/src/utils.cpp | 19 ++---- utils/scope.h | 106 ------------------------------- 5 files changed, 15 insertions(+), 138 deletions(-) delete mode 100644 utils/scope.h diff --git a/plugins/SpellChecker/src/ardialog.cpp b/plugins/SpellChecker/src/ardialog.cpp index a5948d3ec7..eec0687100 100644 --- a/plugins/SpellChecker/src/ardialog.cpp +++ b/plugins/SpellChecker/src/ardialog.cpp @@ -98,9 +98,7 @@ static LRESULT CALLBACK OnlyCharsEditProc(HWND hwnd, UINT msg, WPARAM wParam, LP case WM_PASTE: wchar_t text[256]; GetWindowText(hwnd, text, _countof(text)); - - scoped_free dest = data->dict->autoReplace->filterText(text); - SetWindowText(hwnd, dest); + SetWindowText(hwnd, ptrW(data->dict->autoReplace->filterText(text))); break; } @@ -170,10 +168,9 @@ static INT_PTR CALLBACK AddReplacementDlgProc(HWND hwndDlg, UINT msg, WPARAM wPa SendDlgItemMessage(hwndDlg, IDC_OLD, EM_LIMITTEXT, 256, 0); SendDlgItemMessage(hwndDlg, IDC_NEW, EM_LIMITTEXT, 256, 0); - if (!data->find.empty()) { - scoped_free tmp = data->dict->autoReplace->filterText(data->find.c_str()); - SetDlgItemText(hwndDlg, IDC_OLD, tmp); - } + if (!data->find.empty()) + SetDlgItemText(hwndDlg, IDC_OLD, ptrW(data->dict->autoReplace->filterText(data->find.c_str()))); + if (!data->replace.empty()) SetDlgItemText(hwndDlg, IDC_NEW, data->replace.c_str()); diff --git a/plugins/SpellChecker/src/autoreplace.cpp b/plugins/SpellChecker/src/autoreplace.cpp index ea3449e56d..b111862c3b 100644 --- a/plugins/SpellChecker/src/autoreplace.cpp +++ b/plugins/SpellChecker/src/autoreplace.cpp @@ -129,7 +129,7 @@ BOOL AutoReplaceMap::isWordChar(wchar_t c) wchar_t* AutoReplaceMap::autoReplace(const wchar_t * word) { - scoped_free from = wcslwr(wcsdup(word)); + ptrW from(wcslwr(mir_wstrdup(word))); if (m_replacements.find(from.get()) == m_replacements.end()) return nullptr; @@ -140,7 +140,7 @@ wchar_t* AutoReplaceMap::autoReplace(const wchar_t * word) if (ar.useVariables) to = variables_parsedup((wchar_t *)ar.replace.c_str(), (wchar_t *)word, NULL); else - to = wcsdup(ar.replace.c_str()); + to = mir_wstrdup(ar.replace.c_str()); // Wich case to use? size_t len = mir_wstrlen(word); @@ -166,7 +166,7 @@ wchar_t* AutoReplaceMap::autoReplace(const wchar_t * word) wchar_t* AutoReplaceMap::filterText(const wchar_t *find) { - wchar_t *ret = wcsdup(find); + wchar_t *ret = mir_wstrdup(find); int len = mir_wstrlen(ret); int pos = 0; for (int i = 0; i < len; i++) @@ -178,8 +178,7 @@ wchar_t* AutoReplaceMap::filterText(const wchar_t *find) void AutoReplaceMap::add(const wchar_t * aFrom, const wchar_t * to, BOOL useVariables) { - scoped_free from = filterText(aFrom); - + ptrW from(filterText(aFrom)); m_replacements[from.get()] = AutoReplacement(to, useVariables); writeAutoReplaceMap(); @@ -196,7 +195,7 @@ void AutoReplaceMap::setMap(const map &replacemen map::const_iterator it = replacements.begin(); for (; it != replacements.end(); it++) { - scoped_free from = filterText(it->first.c_str()); + ptrW from(filterText(it->first.c_str())); m_replacements[from.get()] = it->second; } diff --git a/plugins/SpellChecker/src/stdafx.h b/plugins/SpellChecker/src/stdafx.h index 2b7fd394f8..033c84e806 100644 --- a/plugins/SpellChecker/src/stdafx.h +++ b/plugins/SpellChecker/src/stdafx.h @@ -54,9 +54,7 @@ using namespace std; #include #include <../../utils/mir_options.h> -//#include <../../utils/tstring.h> #include <../../utils/utf8_helpers.h> -#include <../../utils/scope.h> #include @@ -130,7 +128,7 @@ struct Dialog static BOOL CenterParent(HWND hwnd); wchar_t *lstrtrim(wchar_t *str); -BOOL lstreq(wchar_t *a, wchar_t *b, size_t len = -1); + inline BOOL IsNumber(wchar_t c) { return c >= '0' && c <= '9'; @@ -152,7 +150,7 @@ LRESULT CALLBACK MenuWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); void ModifyIcon(Dialog *dlg); BOOL GetWordCharRange(Dialog *dlg, CHARRANGE &sel, wchar_t *text, size_t text_len, int &first_char); -wchar_t *GetWordUnderPoint(Dialog *dlg, POINT pt, CHARRANGE &sel); +wchar_t* GetWordUnderPoint(Dialog *dlg, POINT pt, CHARRANGE &sel); int GetClosestLanguage(wchar_t *lang_name); diff --git a/plugins/SpellChecker/src/utils.cpp b/plugins/SpellChecker/src/utils.cpp index 57f29e47ff..e87ab53616 100644 --- a/plugins/SpellChecker/src/utils.cpp +++ b/plugins/SpellChecker/src/utils.cpp @@ -1000,7 +1000,7 @@ BOOL GetWordCharRange(Dialog *dlg, CHARRANGE &sel, wchar_t *text, size_t text_le return has_valid_char; } -wchar_t *GetWordUnderPoint(Dialog *dlg, POINT pt, CHARRANGE &sel) +wchar_t* GetWordUnderPoint(Dialog *dlg, POINT pt, CHARRANGE &sel) { // Get text if (dlg->re->GetTextLength() <= 0) @@ -1018,10 +1018,9 @@ wchar_t *GetWordUnderPoint(Dialog *dlg, POINT pt, CHARRANGE &sel) // copy the word text[sel.cpMax - first_char] = '\0'; - return wcsdup(&text[sel.cpMin - first_char]); + return mir_wstrdup(&text[sel.cpMin - first_char]); } - void AppendSubmenu(HMENU hMenu, HMENU hSubMenu, wchar_t *name) { MENUITEMINFO mii = { 0 }; @@ -1129,7 +1128,7 @@ void FoundWrongWord(wchar_t *word, CHARRANGE pos, void *param) p->count++; - AddMenuForWord(p->dlg, wcsdup(word), pos, p->dlg->hWrongWordsSubMenu, TRUE, WORD_MENU_ID_BASE * p->count); + AddMenuForWord(p->dlg, word, pos, p->dlg->hWrongWordsSubMenu, TRUE, WORD_MENU_ID_BASE * p->count); } void AddItemsToMenu(Dialog *dlg, HMENU hMenu, POINT pt, HWND hwndOwner) @@ -1174,7 +1173,7 @@ void AddItemsToMenu(Dialog *dlg, HMENU hMenu, POINT pt, HWND hwndOwner) } else { CHARRANGE sel; - wchar_t *word = GetWordUnderPoint(dlg, pt, sel); + ptrW word(GetWordUnderPoint(dlg, pt, sel)); if (word != nullptr && !dlg->lang->spell(word)) { InsertMenu(hMenu, 0, MF_BYPOSITION | MF_SEPARATOR, 0, nullptr); AddMenuForWord(dlg, word, sel, hMenu, FALSE, WORD_MENU_ID_BASE); @@ -1559,13 +1558,3 @@ wchar_t* lstrtrim(wchar_t *str) return str; } - -BOOL lstreq(wchar_t *a, wchar_t *b, size_t len) -{ - a = CharLower(wcsdup(a)); - b = CharLower(wcsdup(b)); - BOOL ret = len ? !wcsncmp(a, b, len) : !mir_wstrcmp(a, b); - free(a); - free(b); - return ret; -} diff --git a/utils/scope.h b/utils/scope.h deleted file mode 100644 index 961484e4d7..0000000000 --- a/utils/scope.h +++ /dev/null @@ -1,106 +0,0 @@ -/* -Copyright (C) 2009 Ricardo Pescuma Domenecci - -This is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public -License as published by the Free Software Foundation; either -version 2 of the License, or (at your option) any later version. - -This is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with this file; see the file license.txt. If -not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. -*/ - - -#ifndef __SCOPE_H__ -# define __SCOPE_H__ - - -#define DEFINE_SCOPED_TYPE(_NAME_, _DELETE_) \ - template \ - class _NAME_ \ - { \ - _NAME_(_NAME_ &); \ - _NAME_ & operator=(_NAME_ &); \ - \ - public: \ - _NAME_(T *t = NULL) : p(t) {} \ - \ - ~_NAME_() \ - { \ - release(); \ - } \ - \ - T * operator=(T *t) \ - { \ - release(); \ - p = t; \ - return t; \ - } \ - \ - operator T*() const \ - { \ - return p; \ - } \ - \ - bool operator==(T *t) const \ - { \ - return p == t; \ - } \ - \ - bool operator!=(T *t) const \ - { \ - return p != t; \ - } \ - \ - T *get() const \ - { \ - return p; \ - } \ - \ - void release() \ - { \ - if (p != NULL) \ - dealoc(p); \ - p = NULL; \ - } \ - \ - T* detach() \ - { \ - T *ret = p; \ - p = NULL; \ - return ret; \ - } \ - \ - protected: \ - T *p; \ - \ - void dealoc(T *ptr) \ - { \ - _DELETE_; \ - } \ - }; \ - \ - template \ - inline bool operator==(T* p, const _NAME_ &b) { \ - return p == b.get(); \ - } \ - \ - template \ - inline bool operator!=(T* p, const _NAME_ &b) { \ - return p != b.get(); \ - } - -DEFINE_SCOPED_TYPE(scoped_ptr, delete ptr) -DEFINE_SCOPED_TYPE(scoped_array, delete[] ptr) -DEFINE_SCOPED_TYPE(scoped_free, free(ptr)) -DEFINE_SCOPED_TYPE(scoped_mir_free, mir_free(ptr)) - - -#endif // __SCOPE_H__ -- cgit v1.2.3