Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #11542: FP memleak when using memcpy() with pointer #6792

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,42 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
while (Token::Match(ftok, "%name% :: %name%"))
ftok = ftok->tokAt(2);

if (const Library::Function *libFunc = mSettings->library.getFunction(ftok)) {
using ArgumentChecks = Library::ArgumentChecks;
using Direction = ArgumentChecks::Direction;
const std::vector<const Token *> args = getArguments(ftok);
const std::map<int, ArgumentChecks> &argChecks = libFunc->argumentChecks;
bool hasOutParam = false;
for (const auto &pair : argChecks) {
hasOutParam |= std::any_of(pair.second.direction.cbegin(), pair.second.direction.cend(), [&](const Direction dir) {
return dir == Direction::DIR_OUT;
});
if (hasOutParam)
break;
}
if (hasOutParam) {
for (int i = 0; i < args.size(); i++) {
if (!argChecks.count(i + 1))
continue;
const ArgumentChecks argCheck = argChecks.at(i + 1);
bool isInParam = std::any_of(argCheck.direction.cbegin(), argCheck.direction.cend(), [&](const Direction dir) {
danmar marked this conversation as resolved.
Show resolved Hide resolved
return dir == Direction::DIR_IN;
});
if (!isInParam)
continue;
const Token *inTok = args[i];
int indirect = 0;
while (inTok->isUnaryOp("&")) {
inTok = inTok->astOperand1();
indirect++;
}
if (inTok->isVariable() && indirect) {
varInfo.erase(inTok->varId());
}
}
}
}

auto isAssignment = [](const Token* varTok) -> const Token* {
if (varTok->varId()) {
const Token* top = varTok;
Expand Down
21 changes: 21 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class TestLeakAutoVar : public TestFixture {
TEST_CASE(assign25);
TEST_CASE(assign26);

TEST_CASE(memcpy1); // #11542
TEST_CASE(memcpy2);

TEST_CASE(isAutoDealloc);

TEST_CASE(realloc1);
Expand Down Expand Up @@ -633,6 +636,24 @@ class TestLeakAutoVar : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void memcpy1() { // #11542
const Settings s = settingsBuilder().library("std.cfg").library("posix.cfg").build();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you don't need library("posix")

check("void f(char** old, char* value) {\n"
" char *str = strdup(value);\n"
" memcpy(old, &str, sizeof(char*));\n"
"}\n", &s);
ASSERT_EQUALS("", errout_str());
}

void memcpy2() {
const Settings s = settingsBuilder().library("std.cfg").library("posix.cfg").build();
check("void f(char* old, char* value, size_t len) {\n"
" char *str = strdup(value);\n"
" memcpy(old, str, len);\n"
"}\n", &s);
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: str\n", errout_str());
}

void isAutoDealloc() {
check("void f() {\n"
" char *p = new char[100];"
Expand Down
Loading