Skip to content

[clang] Fix dangling false positives for conditional operators. #120233

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

Merged
merged 1 commit into from
Dec 20, 2024
Merged
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@ Improvements to Clang's diagnostics
return ptr + index < ptr; // warning
}

- Fix -Wdangling false positives on conditional operators (#120206).

Improvements to Clang's time-trace
----------------------------------

Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Sema/CheckExprLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,15 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
// Temp().ptr; // Here ptr might not dangle.
if (isa<MemberExpr>(Arg->IgnoreImpCasts()))
return;
// Avoid false positives when the object is constructed from a conditional
// operator argument. A common case is:
// // 'ptr' might not be owned by the Owner object.
// std::string_view s = cond() ? Owner().ptr : sv;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

note: I keep the code simple here, and heuristic logic is not perfect and may lead to false negatives, as it filters out more cases than intended. The major case std::string_view sv = cond ? "123" : std::string(); is still covered, I think this is a right tradeoff.

Copy link
Collaborator

@Xazax-hun Xazax-hun Dec 17, 2024

Choose a reason for hiding this comment

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

I am a bit confused to be honest. Are there any other contexts where Owner().ptr is problematic? I'd expect our analysis to behave the same for a subexpression like that regardless the context. So I am surprised we need to insert special logic for the ternary operator.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am a bit confused to be honest. Are there any other contexts where Owner().ptr is problematic? I'd expect our analysis to behave the same for a subexpression like that regardless the context. So I am surprised we need to insert special logic for the ternary operator.

Possibly, but I don’t know of any concrete examples (it’s hard to judge). We have another ad-hoc filter at the end, IsGslPtrValueFromGslTempOwner, which filters out cases where the GSL pointer doesn’t originate from a GSL owner. This works well for simple and common cases, but when combined with lifetimebound, the behavior becomes tricky.

The current fix extends the MemberExpr logic (L583) to handle cases like GSLPointer pointer(Owner().ptr);, but it doesn’t yet address cases like GSLPointer pointer(Cond ? Owner().ptr : GSLPointer());. I think this fix is a reasonable extension to address the issue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

but it doesn’t yet address cases like GSLPointer pointer(Cond ? Owner().ptr : GSLPointer())

I am wondering if it was possible to use the same or similar MemberExpr filter when we drill down to the branches of the conditional operator. If it is too hard to do, I am OK with the current solution. But to me expanding the MemberExpr logic to handle more expressions sounds more natural than adding logic for a new expression type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am wondering if it was possible to use the same or similar MemberExpr filter when we drill down to the branches of the conditional operator. If it is too hard to do,

This was my first approach. We could add a special MemberExpr filter inside the do-while loop in visitLocalsRetainedByReferenceBinding, but I don’t think it’s a good idea:

  • the filtering logic is GSL-pointer specific, and visitLocalsRetainedByReferenceBinding shouldn’t be aware of it; (layering violation)
  • adding such filtering causes new false negatives in cases like const string_view& sv = Owner().sv;.
  • it would scatter the special filtering logic into another place, making the code harder to reason about.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is const string_view& sv = Owner().sv; a false negative? We cannot actually know if sv is actually owned or not because we have no lifetimebound annotation and the gsl owner does not tell us anything about fields, only about conversions/ctors.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, never mind, I missed the const ref!

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, with all of these being said I am OK with the current approach.

if (const auto *Cond =
dyn_cast<AbstractConditionalOperator>(Arg->IgnoreImpCasts());
Cond && isPointerLikeType(Cond->getType()))
return;

auto ReturnType = Callee->getReturnType();

// Once we initialized a value with a non gsl-owner reference, it can no
Expand Down
29 changes: 29 additions & 0 deletions clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,3 +777,32 @@ void test4() {
}

} // namespace LifetimeboundInterleave

namespace GH120206 {
struct S {
std::string_view s;
};

struct [[gsl::Owner]] Q1 {
const S* get() const [[clang::lifetimebound]];
};
std::string_view test1(int c, std::string_view sv) {
std::string_view k = c > 1 ? Q1().get()->s : sv;
if (c == 1)
return c > 1 ? Q1().get()->s : sv;
Q1 q;
return c > 1 ? q.get()->s : sv;
}

struct Q2 {
const S* get() const [[clang::lifetimebound]];
};
std::string_view test2(int c, std::string_view sv) {
std::string_view k = c > 1 ? Q2().get()->s : sv;
if (c == 1)
return c > 1 ? Q2().get()->s : sv;
Q2 q;
return c > 1 ? q.get()->s : sv;
}

} // namespace GH120206
Loading