@@ -149,22 +149,24 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
149149 return Malloc;
150150
151151 // Does tok2 point on "g_malloc", "g_strdup", ..
152- static const char * const gmallocfunc[] = {
153- " g_new" ,
154- " g_new0" ,
155- " g_try_new" ,
156- " g_try_new0" ,
157- " g_malloc" ,
158- " g_malloc0" ,
159- " g_try_malloc" ,
160- " g_try_malloc0" ,
161- " g_strdup" ,
162- " g_strndup" ,
163- " g_strdup_printf"
164- };
165- for (unsigned int i = 0 ; i < sizeof (gmallocfunc)/sizeof (*gmallocfunc); i++) {
166- if (tok2->str () == gmallocfunc[i])
167- return gMalloc ;
152+ if (standards.gtk ) {
153+ static const char * const gmallocfunc[] = {
154+ " g_new" ,
155+ " g_new0" ,
156+ " g_try_new" ,
157+ " g_try_new0" ,
158+ " g_malloc" ,
159+ " g_malloc0" ,
160+ " g_try_malloc" ,
161+ " g_try_malloc0" ,
162+ " g_strdup" ,
163+ " g_strndup" ,
164+ " g_strdup_printf"
165+ };
166+ for (unsigned int i = 0 ; i < sizeof (gmallocfunc)/sizeof (*gmallocfunc); i++) {
167+ if (tok2->str () == gmallocfunc[i])
168+ return gMalloc ;
169+ }
168170 }
169171
170172 if (Token::Match (tok2, " new struct| %type% [;()]" ) ||
@@ -180,17 +182,19 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
180182 if (Token::Match (tok2, " fopen|tmpfile|g_fopen (" ))
181183 return File;
182184
183- if (Token::Match (tok2, " open|openat|creat|mkstemp|mkostemp (" )) {
184- // simple sanity check of function parameters..
185- // TODO: Make such check for all these functions
186- unsigned int num = countParameters (tok2);
187- if (tok2->str () == " open" && num != 2 && num != 3 )
188- return No;
185+ if (standards.posix ) {
186+ if (Token::Match (tok2, " open|openat|creat|mkstemp|mkostemp (" )) {
187+ // simple sanity check of function parameters..
188+ // TODO: Make such check for all these functions
189+ unsigned int num = countParameters (tok2);
190+ if (tok2->str () == " open" && num != 2 && num != 3 )
191+ return No;
189192
190- // is there a user function with this name?
191- if (tokenizer && Token::findmatch (tokenizer->tokens (), (" %type% *|&| " + tok2->str ()).c_str ()))
192- return No;
193- return Fd;
193+ // is there a user function with this name?
194+ if (tokenizer && Token::findmatch (tokenizer->tokens (), (" %type% *|&| " + tok2->str ()).c_str ()))
195+ return No;
196+ return Fd;
197+ }
194198 }
195199
196200 if (Token::simpleMatch (tok2, " popen (" ))
@@ -220,11 +224,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
220224 return functionReturnType (func, callstack);
221225}
222226
223- static bool isPosixAllocationType (const CheckMemoryLeak::AllocType allocType)
224- {
225- return (allocType == CheckMemoryLeak::Fd || allocType == CheckMemoryLeak::Pipe || allocType == CheckMemoryLeak::Dir);
226- }
227-
228227
229228CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType (const Token *tok2, unsigned int varid) const
230229{
@@ -273,22 +272,26 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
273272 Token::Match (tok, " realloc ( %varid% , 0 ) ;" , varid))
274273 return Malloc;
275274
276- if (Token::Match (tok, " g_free ( %varid% ) ;" , varid) ||
277- Token::Match (tok, " g_free ( %varid% -" , varid))
278- return gMalloc ;
275+ if (standards.gtk ) {
276+ if (Token::Match (tok, " g_free ( %varid% ) ;" , varid) ||
277+ Token::Match (tok, " g_free ( %varid% -" , varid))
278+ return gMalloc ;
279+ }
279280
280281 if (Token::Match (tok, " fclose ( %varid% )" , varid) ||
281282 Token::simpleMatch (tok, " fcloseall ( )" ))
282283 return File;
283284
284- if (Token::Match (tok, " close ( %varid% )" , varid))
285- return Fd;
285+ if (standards.posix ) {
286+ if (Token::Match (tok, " close ( %varid% )" , varid))
287+ return Fd;
286288
287- if (Token::Match (tok, " pclose ( %varid% )" , varid))
288- return Pipe;
289+ if (Token::Match (tok, " pclose ( %varid% )" , varid))
290+ return Pipe;
289291
290- if (Token::Match (tok, " closedir ( %varid% )" , varid))
291- return Dir;
292+ if (Token::Match (tok, " closedir ( %varid% )" , varid))
293+ return Dir;
294+ }
292295
293296 return No;
294297}
@@ -887,9 +890,6 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
887890 }
888891 }
889892
890- if (isPosixAllocationType (alloc) && !_settings->standards .posix )
891- alloc = CheckMemoryLeak::No;
892-
893893 // don't check classes..
894894 if (alloc == CheckMemoryLeak::New) {
895895 if (Token::Match (tok->tokAt (2 ), " new struct| %type% [(;]" )) {
@@ -979,9 +979,6 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
979979
980980 AllocType dealloc = getDeallocationType (tok, varid);
981981
982- if (isPosixAllocationType (dealloc) && !_settings->standards .posix )
983- dealloc = CheckMemoryLeak::No;
984-
985982 if (dealloc != No && tok->str () == " fcloseall" && alloctype != dealloc)
986983 // TODO: this assignment is redundant, should be fixed
987984 /* dealloc = No*/ ;
0 commit comments