From 5e0c012d677635e13cbfcf6730f2deafdc38c025 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 3 Jan 2014 15:57:52 +0000 Subject: - memory leak fixes - code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@7479 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Variables/src/parse_alias.cpp | 78 +++++++++++++++++------------------ 1 file changed, 38 insertions(+), 40 deletions(-) (limited to 'plugins/Variables/src/parse_alias.cpp') diff --git a/plugins/Variables/src/parse_alias.cpp b/plugins/Variables/src/parse_alias.cpp index 403bc2a7a0..848a91b3d0 100644 --- a/plugins/Variables/src/parse_alias.cpp +++ b/plugins/Variables/src/parse_alias.cpp @@ -2,7 +2,7 @@ Variables Plugin for Miranda-IM (www.miranda-im.org) Copyright 2003-2006 P. Boon - This program is mir_free software; you can redistribute it and/or modify + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -36,33 +36,33 @@ static ALIASREGISTER *searchAliasRegister(TCHAR *szAlias) return NULL; mir_cslock lck(csAliasRegister); - for (int i=0; i < arAliases.getCount(); i++) - if (!_tcscmp(arAliases[i]->szAlias, szAlias)) - return arAliases[i]; + for (int i = 0; i < arAliases.getCount(); i++) + if (!_tcscmp(arAliases[i]->szAlias, szAlias)) + return arAliases[i]; return NULL; } static TCHAR *replaceArguments(TCHAR *res, TCHAR *tArg, TCHAR *rArg) { - if ( _tcslen(tArg) == 0) + if (_tcslen(tArg) == 0) return res; unsigned int cur = 0, ecur = 0; - while (*(res+cur) != 0) { - if ((*(res+cur) == '(') || (*(res+cur) == ',')) { + while (*(res + cur) != 0) { + if ((*(res + cur) == '(') || (*(res + cur) == ',')) { ecur = ++cur; - while ( (*(res+ecur) != ')') && (*(res+ecur) != ',')) + while ((*(res + ecur) != ')') && (*(res + ecur) != ',')) ecur++; - if (((signed int)_tcslen(tArg) == (ecur-cur)) && (!_tcsncmp(tArg, res+cur, _tcslen(tArg)))) { - if ( _tcslen(rArg) > _tcslen(tArg)) { - res = (TCHAR*)mir_realloc(res, (_tcslen(res) + (_tcslen(rArg)-_tcslen(tArg)) + 1)*sizeof(TCHAR)); + if (((signed int)_tcslen(tArg) == (ecur - cur)) && (!_tcsncmp(tArg, res + cur, _tcslen(tArg)))) { + if (_tcslen(rArg) > _tcslen(tArg)) { + res = (TCHAR*)mir_realloc(res, (_tcslen(res) + (_tcslen(rArg) - _tcslen(tArg)) + 1)*sizeof(TCHAR)); if (res == NULL) return NULL; } - MoveMemory(res+ecur+(_tcslen(rArg)-_tcslen(tArg)), res+ecur, (_tcslen(res+ecur)+1)*sizeof(TCHAR)); - _tcsncpy(res+cur, rArg, _tcslen(rArg)); + MoveMemory(res + ecur + (_tcslen(rArg) - _tcslen(tArg)), res + ecur, (_tcslen(res + ecur) + 1)*sizeof(TCHAR)); + _tcsncpy(res + cur, rArg, _tcslen(rArg)); } } cur++; @@ -74,12 +74,12 @@ static TCHAR *replaceArguments(TCHAR *res, TCHAR *tArg, TCHAR *rArg) static TCHAR *parseTranslateAlias(ARGUMENTSINFO *ai) { ALIASREGISTER *areg = searchAliasRegister(ai->targv[0]); - if (areg == NULL || areg->argc != ai->argc-1) + if (areg == NULL || areg->argc != ai->argc - 1) return NULL; TCHAR *res = mir_tstrdup(areg->szTranslation); - for (unsigned i=0; i < areg->argc; i++) { - res = replaceArguments(res, areg->argv[i], ai->targv[i+1]); + for (unsigned i = 0; i < areg->argc; i++) { + res = replaceArguments(res, areg->argv[i], ai->targv[i + 1]); if (res == NULL) return NULL; } @@ -89,27 +89,26 @@ static TCHAR *parseTranslateAlias(ARGUMENTSINFO *ai) static int addToAliasRegister(TCHAR *szAlias, unsigned int argc, TCHAR** argv, TCHAR *szTranslation) { - if (szAlias == NULL || szTranslation == NULL || _tcslen(szAlias) == 0 ) + if (szAlias == NULL || szTranslation == NULL || _tcslen(szAlias) == 0) return -1; mir_cslock lck(csAliasRegister); - for (int i=0; i < arAliases.getCount(); i++) { + for (int i = 0; i < arAliases.getCount(); i++) { ALIASREGISTER *p = arAliases[i]; - if ( _tcscmp(p->szAlias, szAlias)) + if (_tcscmp(p->szAlias, szAlias)) continue; mir_free(p->szTranslation); p->szTranslation = mir_tstrdup(szTranslation); - for (unsigned j=0; j < p->argc; j++) - if (p->argv[j] != NULL) - mir_free(p->argv[j]); + for (unsigned j = 0; j < p->argc; j++) + mir_free(p->argv[j]); p->argc = argc; p->argv = (TCHAR**)mir_realloc(p->argv, argc * sizeof(TCHAR*)); if (p->argv == NULL) return -1; - for (unsigned j=0; j < argc; j++) { + for (unsigned j = 0; j < argc; j++) { if (argv[j] != NULL) p->argv[j] = mir_tstrdup(argv[j]); else @@ -126,7 +125,7 @@ static int addToAliasRegister(TCHAR *szAlias, unsigned int argc, TCHAR** argv, T if (p->argv == NULL) return -1; - for (unsigned j=0; j < p->argc; j++) { + for (unsigned j = 0; j < p->argc; j++) { if (argv[j] != NULL) p->argv[j] = mir_tstrdup(argv[j]); else @@ -139,8 +138,6 @@ static int addToAliasRegister(TCHAR *szAlias, unsigned int argc, TCHAR** argv, T static TCHAR *parseAddAlias(ARGUMENTSINFO *ai) { int res; - int argc, i; - TCHAR **argv; char *szHelp, *szArgsA; if (ai->argc != 3) @@ -150,40 +147,39 @@ static TCHAR *parseAddAlias(ARGUMENTSINFO *ai) while (isValidTokenChar(*cur)) cur++; - TCHAR *alias = (TCHAR*)mir_calloc(((cur-ai->targv[1])+1)*sizeof(TCHAR)); - if (alias == NULL) - return NULL; + ptrT alias(mir_tstrndup(ai->targv[1], cur - ai->targv[1])); - _tcsncpy(alias, ai->targv[1], (cur-ai->targv[1])); + int argc; + TCHAR **argv; getArguments(cur, &argv, &argc); deRegisterToken(alias); addToAliasRegister(alias, argc, argv, ai->targv[2]); TCHAR *szArgs = NULL; - for (i=0; i < argc; i++) { + for (int i = 0; i < argc; i++) { if (i == 0) - szArgs = (TCHAR*)mir_calloc(( _tcslen(argv[i])+2)*sizeof(TCHAR)); + szArgs = (TCHAR*)mir_calloc((_tcslen(argv[i]) + 2)*sizeof(TCHAR)); else szArgs = (TCHAR*)mir_realloc(szArgs, (_tcslen(szArgs) + _tcslen(argv[i]) + 2)*sizeof(TCHAR)); _tcscat(szArgs, argv[i]); - if (i != argc-1) + if (i != argc - 1) _tcscat(szArgs, _T(",")); } - if ((szArgs != NULL) && (argc > 0)) { - szArgsA = mir_t2a(szArgs); + if (szArgs != NULL && argc > 0) { + szArgsA = mir_t2a(szArgs); size_t size = 32 + strlen(szArgsA); szHelp = (char *)mir_alloc(size); memset(szHelp, '\0', 32 + strlen(szArgsA)); mir_snprintf(szHelp, size, LPGEN("Alias")"\t(%s)\t"LPGEN("user defined"), szArgsA); - res = registerIntToken(alias, parseTranslateAlias, TRF_FUNCTION|TRF_UNPARSEDARGS, szHelp); + res = registerIntToken(alias, parseTranslateAlias, TRF_FUNCTION | TRF_UNPARSEDARGS, szHelp); mir_free(szArgsA); } else { - szHelp = ( char* )mir_alloc(32); + szHelp = (char*)mir_alloc(32); memset(szHelp, '\0', 32); mir_snprintf(szHelp, 32, LPGEN("Alias")"\t\t"LPGEN("user defined")); - res = registerIntToken(alias, parseTranslateAlias, TRF_FIELD|TRF_UNPARSEDARGS, szHelp); + res = registerIntToken(alias, parseTranslateAlias, TRF_FIELD | TRF_UNPARSEDARGS, szHelp); } mir_free(szArgs); mir_free(szHelp); @@ -192,7 +188,7 @@ static TCHAR *parseAddAlias(ARGUMENTSINFO *ai) int registerAliasTokens() { - registerIntToken(_T(ADDALIAS), parseAddAlias, TRF_FUNCTION|TRF_UNPARSEDARGS, LPGEN("Variables")"\t(x,y)\t"LPGEN("stores y as alias named x"));//TRF_UNPARSEDARGS); + registerIntToken(_T(ADDALIAS), parseAddAlias, TRF_FUNCTION | TRF_UNPARSEDARGS, LPGEN("Variables")"\t(x,y)\t"LPGEN("stores y as alias named x"));//TRF_UNPARSEDARGS); InitializeCriticalSection(&csAliasRegister); return 0; } @@ -201,8 +197,10 @@ void unregisterAliasTokens() { DeleteCriticalSection(&csAliasRegister); - for (int i=0; i < arAliases.getCount(); i++) { + for (int i = 0; i < arAliases.getCount(); i++) { ALIASREGISTER *p = arAliases[i]; + for (unsigned j = 0; j < p->argc; j++) + mir_free(p->argv[j]); mir_free(p->argv); mir_free(p->szAlias); mir_free(p->szTranslation); -- cgit v1.2.3