In CheckLeakAutoVarImpl::ret() the returned variable is only recognized when the token before it is one of return ( { , * (lib/checkleakautovar.cpp:1208) and the token after it is one of ) } ; , + (lib/checkleakautovar.cpp:1225). Neither list contains ? or :, so ? p : and : p ; are not seen as returning p. The conditional operator in an assignment (x = p ? p : nullptr;) was fixed in #8235 (test assign26), but the return statement still has the old token lists. Accepting ? and : in those two lists, or walking the AST of the return expression, should fix it.
Reproduced with 2.20.1 and current main (530e59f).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
In both functions
pis returned to the caller on every path, so nothing leaks. The same check written as anifis not reported:In
CheckLeakAutoVarImpl::ret()the returned variable is only recognized when the token before it is one ofreturn ( { , *(lib/checkleakautovar.cpp:1208) and the token after it is one of) } ; , +(lib/checkleakautovar.cpp:1225). Neither list contains?or:, so? p :and: p ;are not seen as returningp. The conditional operator in an assignment (x = p ? p : nullptr;) was fixed in #8235 (testassign26), but the return statement still has the old token lists. Accepting?and:in those two lists, or walking the AST of the return expression, should fix it.Reproduced with 2.20.1 and current main (530e59f).
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15233