Skip to content

[Clang SA] Potential memory leak when init struct field during construction #60896

@igagis

Description

@igagis

To reproduce:

Check out the minimal reproducer example at:
https://github.com/igagis/clang-tidy-test/blob/master/src/main.cpp

the relevant part of the code is:

struct result{
    std::shared_ptr<std::string> p;
    bool some_other_field;
};

result potential_memory_leak_2(bool arg){
    result ret{.p = std::make_shared<std::string>("Hello world!")};

    if(arg){
        throw std::runtime_error("error");
    }

    return ret;
}

run clang-tidy with all checks on.

Or just refer to the minimal reproducer's CI run:
https://github.com/igagis/clang-tidy-test/actions/runs/4231046017/jobs/7349082519

Expected result:

No false-positive potential memory leaks found

Actual result:

The following error is reported by clang-tidy:

src/main.cpp:26:1: error: Potential leak of memory pointed to by field '_M_pi' [clang-analyzer-cplusplus.NewDeleteLeaks,-warnings-as-errors]

Note

the minimal reproducer also demonstrates another false-positive, which is already reported by #55219

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions