Skip to content

False positive: ConstParameterNotModified.Pointer on a pointee modified in a lambda #188

Open

Description

@SizzleUnrlsd

ConstParameterNotModified.Pointer suggests const for a pointer parameter whose pointee is modified through a lambda that captures the parameter by reference.

Example

lambda-modifies-pointee.cpp:

struct Counter
{
    unsigned long count = 0;
};

int count_through_lambda(int x, Counter* counter)
{
    const auto add = [&](int value)
    {
        if (counter)
            ++counter->count;
        return value;
    };
    return add(x) + add(1);
}

With the analyzer built from main 112ffcf:

$ stack_usage_analyzer lambda-modifies-pointee.cpp
	at line 6, column 0
	[ !Info! ] ConstParameterNotModified.Pointer: parameter 'counter' in function 'count_through_lambda(int, Counter*)' is never used to modify the pointed object
		 ↳ current type: Counter *counter
		 ↳ suggested type: const Counter *counter

Why it is wrong

The lambda increments counter->count. With the suggested type, the file no longer compiles:

lambda-modifies-pointee.cpp:11:13: error: cannot assign to variable 'counter' with const-qualified type 'const Counter *'

Where it shows

The self-analysis reports it on computeSummary (src/analysis/ownership/OwnershipEngine.cpp), whose SummaryWork* work is incremented in the lambda solveCounted (#184).

Alert: https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/256 (#187).

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions