Skip to content

Fix #14949: FP leakNoVarFunctionCall when passing resource to constructor - #8765

Open
aadanen wants to merge 4 commits into
cppcheck-opensource:mainfrom
aadanen:fix14949
Open

Fix #14949: FP leakNoVarFunctionCall when passing resource to constructor#8765
aadanen wants to merge 4 commits into
cppcheck-opensource:mainfrom
aadanen:fix14949

Conversation

@aadanen

@aadanen aadanen commented Jul 29, 2026

Copy link
Copy Markdown

Remove Variable and AllocType type checking when searching for unreleased arguments and when checking if member variables are deallocated in destructor.

This method is a little heavy handed and might hurt performance, but I am unsure how we could be more precise. It would be nice if we could check "If var.isResource() or var.needsToBeDeallocated()" but that would be a different PR i think.

@aadanen

aadanen commented Jul 29, 2026

Copy link
Copy Markdown
Author

clang-tidy/build fails with
/home/runner/work/cppcheck/cppcheck/lib/checkmemoryleak.cpp:1049:13: error: do not use 'else' after 'continue' [readability-else-after-return,-warnings-as-errors]
1049 | else {
| ^
which will be easy to fix

sanitizers/build fails with a timeout. As I anticipated this solution is too slow. I will think about how to make it faster.

@chrchr-github

chrchr-github commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

sanitizers/build fails with a timeout. As I anticipated this solution is too slow. I will think about how to make it faster.

It does that all the time, I'll rerun it later...

Comment thread lib/checkmemoryleak.cpp Outdated
for (const Scope * scope : symbolDatabase->classAndStructScopes) {
for (const Variable &var : scope->varlist) {
if (!var.isStatic() && (var.isPointer() || var.isPointerArray())) {
if (!var.isStatic()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think this has any affect in this case. This check only considers members that are allocated in the constructor.

@aadanen aadanen Jul 30, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You are correct that it is irrelevant to this bug and I will revert the change. I made the change because I was a little confused and it is "wrong" for the same reason. Sorry!

I still think the change should be made because it makes the following enhancement:

struct S {
    explicit S(char *name) {
        m_fd = mkstemp(name); // file is not closed in destructor
    }
    int m_fd = -1;
};

S g(char *name) {
    return S(name);
}

void f(char* ptr) {
    S s = g(ptr);
}

(reports nothing) (requires --library=posix)

struct S {
    explicit S() {
        mem = malloc(42); // memory not freed in destructor
    }
    int *mem = NULL;
};

S g() {
    return S();
}

void f() {
    S s = g();
}

(reports unsafeClassError)

but that should be a separate bug and PR because its different and also it would be an "enhancement" instead of a "false positive". Should I make another forum post?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants