Skip to content

Commit

Permalink
Fix #12297 FP: memleak
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 8, 2024
1 parent 6df2e0a commit 2052f5f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,9 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin
if (mSettings->library.getDeallocFuncInfo(tokName)) {
changeAllocStatus(varInfo, dealloc.type == 0 ? allocation : dealloc, tokName, arg);
}
if (allocFunc->arg == argNr && !(arg->variable() && arg->variable()->isArgument() && arg->valueType() && arg->valueType()->pointer > 1)) {
if (allocFunc->arg == argNr &&
!(arg->variable() && arg->variable()->isArgument() && arg->valueType() && arg->valueType()->pointer > 1) &&
(isAddressOf || (arg->valueType() && arg->valueType()->pointer == 2))) {
leakIfAllocated(arg, varInfo);
VarInfo::AllocInfo& varAlloc = varInfo.alloctype[arg->varId()];
varAlloc.type = allocFunc->groupId;
Expand Down
39 changes: 36 additions & 3 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
#include <string>
#include <vector>

class TestLeakAutoVarStrcpy;
class TestLeakAutoVarWindows;

class TestLeakAutoVar : public TestFixture {
public:
TestLeakAutoVar() : TestFixture("TestLeakAutoVar") {}
Expand Down Expand Up @@ -3283,3 +3280,39 @@ class TestLeakAutoVarWindows : public TestFixture {
};

REGISTER_TEST(TestLeakAutoVarWindows)

class TestLeakAutoVarPosix : public TestFixture {
public:
TestLeakAutoVarPosix() : TestFixture("TestLeakAutoVarPosix") {}

private:
const Settings settings = settingsBuilder().library("std.cfg").library("posix.cfg").build();

void check_(const char* file, int line, const char code[]) {
// Clear the error buffer..
errout.str("");

// Tokenize..
Tokenizer tokenizer(settings, this);
std::istringstream istr(code);
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);

// Check for leaks..
runChecks<CheckLeakAutoVar>(tokenizer, this);
}

void run() override {
TEST_CASE(memleak_getline);
}

void memleak_getline() {
check("void f(std::ifstream &is) {\n" // #12297
" std::string str;\n"
" if (getline(is, str, 'x').good()) {};\n"
" if (!getline(is, str, 'x').good()) {};\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};

REGISTER_TEST(TestLeakAutoVarPosix)

0 comments on commit 2052f5f

Please sign in to comment.