Skip to content

[LifetimeSafety] False-negative for std::unique_ptr::get() #162622

@usx95

Description

@usx95
#include <iostream>
#include <memory>
#include <string>
#include <string_view>

void bar(bool cond) {
    std::string* ptr;
    if (cond) {
        std::unique_ptr<std::string> owner =
            std::make_unique<std::string>("abcd");
        ptr = owner.get(); // No error. BAD!
    }
    std::cout << *ptr;
}

https://godbolt.org/z/bdEcKqqfe

AST:

   |   `-BinaryOperator <line:11:9, col:25> 'std::string *' lvalue '='
    |     |-DeclRefExpr <col:9> 'std::string *' lvalue Var 0x268b1d30 'ptr' 'std::string *'
    |     `-CXXMemberCallExpr <col:15, col:25> 'pointer':'std::basic_string<char> *'
    |       `-MemberExpr <col:15, col:21> '<bound member function type>' .get 0x2693e4c8
    |         `-ImplicitCastExpr <col:15> 'const std::unique_ptr<std::basic_string<char>>' lvalue <NoOp>
    |           `-DeclRefExpr <col:15> 'std::unique_ptr<std::string>':'std::unique_ptr<std::basic_string<char>>' lvalue Var 0x268b21c0 'owner' 'std::unique_ptr<std::string>':'std::unique_ptr<std::basic_string<char>>'

Relevant Facts:

    Issue (2 (Path: owner), ToOrigin: 3 (Expr: DeclRefExpr))
    OriginFlow (Dest: 4 (Expr: ImplicitCastExpr), Src: 3 (Expr: DeclRefExpr))
    Use (5 (Decl: ptr), Write)
    OriginFlow (Dest: 5 (Decl: ptr), Src: 6 (Expr: CXXMemberCallExpr))
    Expire (2 (Path: owner))

We are missing facts for CXXMemberCallExpr and MemberExpr. Maybe the get() is not being considered implicitly lifetimebound

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:temporal-safetyIssue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr)false-negativeWarning doesn't fire when it should

    Type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions