Skip to content

Commit 1301cc4

Browse files
committed
CheckLeakAutoVar: --check-library accepts <use> functions
Passing an allocated pointer to a function configured as <use> still gave --check-library: Function f() should have <use>/<leak-ignore> configuration The reference says <use> is there to silence exactly that message, so do not report functions that have it.
1 parent e5f9f00 commit 1301cc4

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎lib/checkleakautovar.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ void CheckLeakAutoVarImpl::deallocReturnError(const Token *tok, const Token *dea
138138
void CheckLeakAutoVarImpl::configurationInfo(const Token* tok, const std::pair<const Token*, VarInfo::Usage>& functionUsage)
139139
{
140140
if (mSettings.checkLibrary && functionUsage.second == VarInfo::USED &&
141-
(!functionUsage.first || !functionUsage.first->function() || !functionUsage.first->function()->hasBody())) {
141+
(!functionUsage.first || !functionUsage.first->function() || !functionUsage.first->function()->hasBody()) &&
142+
!(functionUsage.first && mSettings.library.isUse(mSettings.library.getFunctionName(functionUsage.first)))) {
142143
std::string funcStr = functionUsage.first ? mSettings.library.getFunctionName(functionUsage.first) : "f";
143144
if (funcStr.empty())
144145
funcStr = "unknown::" + functionUsage.first->str();

‎test/testleakautovar.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class TestLeakAutoVar : public TestFixture {
202202
TEST_CASE(configuration4);
203203
TEST_CASE(configuration5);
204204
TEST_CASE(configuration6);
205+
TEST_CASE(configurationUse);
205206

206207
TEST_CASE(ptrptr);
207208

@@ -3102,6 +3103,29 @@ class TestLeakAutoVar : public TestFixture {
31023103
ASSERT_EQUALS("[test.cpp:5:5]: (error) Resource leak: file [resourceLeak]\n", errout_str());
31033104
}
31043105

3106+
void configurationUse() {
3107+
// A function configured with <use> takes care of the memory, so it needs
3108+
// no further configuration.
3109+
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
3110+
"<def format=\"2\">\n"
3111+
" <memory>\n"
3112+
" <alloc>myalloc</alloc>\n"
3113+
" <use>set_data</use>\n"
3114+
" <dealloc>myfree</dealloc>\n"
3115+
" </memory>\n"
3116+
" <function name=\"set_data\">\n"
3117+
" <noreturn>false</noreturn>\n"
3118+
" <arg nr=\"1\"/>\n"
3119+
" </function>\n"
3120+
"</def>\n";
3121+
const Settings settingsUse = settingsBuilder().libraryxml(xmldata).checkLibrary().build();
3122+
check("void f() {\n"
3123+
" char *p = myalloc(10);\n"
3124+
" set_data(p);\n"
3125+
"}\n", settingsUse);
3126+
ASSERT_EQUALS("", errout_str());
3127+
}
3128+
31053129
void configuration6() {
31063130
check("void f() {}\n" // #11198
31073131
"void g() {\n"

0 commit comments

Comments
 (0)