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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
48 changes: 48 additions & 0 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,54 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
while (Token::Match(ftok, "%name% :: %name%"))
ftok = ftok->tokAt(2);

// memcpy / memmove
if (Token::Match(varTok, "memcpy|memmove")) {
const std::vector<const Token*> args = getArguments(varTok);
// too few args for memcpy / memmove call
if (args.size() < 3)
continue;
const Token *dst = args[0];
const Token *src = args[1];

// check that dst arg is pointer to pointer
int dstIndirectionLevel = 0;
while (dst->str() == "*") {
dst = dst->astOperand1();
dstIndirectionLevel--;
}
if (dst->str() == "&") {
dst = dst->astOperand1();
dstIndirectionLevel++;
}
if (!dst->isVariable())
continue;
if (dstIndirectionLevel + dst->variable()->valueType()->pointer != 2)
continue;

// check that src arg is pointer to pointer
int srcIndirectionLevel = 0;
while (src->str() == "*") {
src = src->astOperand1();
srcIndirectionLevel--;
}
if (src->str() == "&") {
src = src->astOperand1();
srcIndirectionLevel++;
}
if (!src->isVariable())
continue;
if (srcIndirectionLevel + src->variable()->valueType()->pointer != 2)
continue;

if (!dst->variable()->isArgument()) {
varInfo.alloctype[dst->varId()].status = VarInfo::AllocStatus::ALLOC;
}

// no multivariable checking currently (see assignment below)
// treat source pointer as unallocated
varInfo.erase(src->varId());
}

auto isAssignment = [](const Token* varTok) -> const Token* {
if (varTok->varId()) {
const Token* top = varTok;
Expand Down
23 changes: 23 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(memcpyWithPtr1); // #11542
TEST_CASE(memcpyWithPtr2);

TEST_CASE(isAutoDealloc);

TEST_CASE(realloc1);
Expand Down Expand Up @@ -631,6 +634,26 @@ class TestLeakAutoVar : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void memcpyWithPtr1() { // #11542
check("#include <string.h>\n"
"void f(char** old, char* value) {\n"
" char *str = strdup(value);\n"
" memcpy(old, &str, sizeof(char*));\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void memcpyWithPtr2() {
check("#include <string.h>\n"
"void f(char* value) {\n"
" char *old = NULL;\n"
" char *str = strdup(value);\n"
" memcpy(&old, &str, sizeof(char*));\n"
"}\n");
// TODO: make this fail
ASSERT_EQUALS("", errout_str());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Running cppcheck on this code on the command line (without any additional options) produces an error,but nothing happens in the test function.

What could the reason for this be?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe std.cfg is not loaded in that test, so strdup() and memcpy() are not recognized. There should be other tests where we load the library.

Copy link
Contributor Author

@ludviggunne ludviggunne Sep 11, 2024

Choose a reason for hiding this comment

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

I'm just matching the token with "memcpy" though, is the library loading really required for that?

Edit: right, the strdup is crucial

}

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