Skip to content

Commit

Permalink
Fix #13126 FP deallocuse with fdopen() (#6823)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Sep 24, 2024
1 parent 0a36cdd commit 3f8244d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,8 @@ const Token * CheckLeakAutoVar::checkTokenInsideExpression(const Token * const t
const std::map<int, VarInfo::AllocInfo>::const_iterator var = varInfo.alloctype.find(tok->varId());
if (var != varInfo.alloctype.end()) {
bool unknown = false;
if (var->second.status == VarInfo::DEALLOC && CheckNullPointer::isPointerDeRef(tok, unknown, *mSettings, /*checkNullArg*/ false) && !unknown) {
if (var->second.status == VarInfo::DEALLOC && !Library::isresource(var->second.type) &&
CheckNullPointer::isPointerDeRef(tok, unknown, *mSettings, /*checkNullArg*/ false) && !unknown) {
deallocUseError(tok, tok->str());
} else if (Token::simpleMatch(tok->tokAt(-2), "= &")) {
varInfo.erase(tok->varId());
Expand Down Expand Up @@ -930,8 +931,10 @@ void CheckLeakAutoVar::changeAllocStatus(VarInfo &varInfo, const VarInfo::AllocI
var->second.allocTok = allocation.allocTok;
}
} else if (allocation.status != VarInfo::NOALLOC && allocation.status != VarInfo::OWNED && !Token::simpleMatch(tok->astTop(), "return")) {
alloctype[arg->varId()].status = VarInfo::DEALLOC;
alloctype[arg->varId()].allocTok = tok;
auto& allocInfo = alloctype[arg->varId()];
allocInfo.status = VarInfo::DEALLOC;
allocInfo.allocTok = tok;
allocInfo.type = allocation.type;
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3334,6 +3334,7 @@ class TestLeakAutoVarPosix : public TestFixture {

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

void memleak_getline() {
Expand All @@ -3344,6 +3345,21 @@ class TestLeakAutoVarPosix : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout_str());
}

void deallocuse_fdopen() {
check("struct FileCloser {\n" // #13126
" void operator()(std::FILE * ptr) const {\n"
" std::fclose(ptr);\n"
" }\n"
"};\n"
"void f(char* fileName) {\n"
" std::unique_ptr<std::FILE, FileCloser> out;\n"
" int fd = mkstemps(fileName, 4);\n"
" if (fd != -1)\n"
" out.reset(fdopen(fd, \"w\"));\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
};

REGISTER_TEST(TestLeakAutoVarPosix)

0 comments on commit 3f8244d

Please sign in to comment.