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

Conversation

hokein
Copy link
Collaborator

@hokein hokein commented Dec 17, 2024

When analyzing a dangling gsl pointer, we currently filter out all field access MemberExpr to avoid common false positives (string_view sv = Temp().sv), However, this filter only applies to direct MemberExpr instances, leaving the conditional operator as an escaping example (GSLPointer pointer(Cond ? Owner().ptr : GSLPointer());).

This patch extends the MemberExpr logic to handle the conditional operator. The heuristic is intentionally simple, which may result in some false negatives. However, it effectively covers common cases like std::string_view sv = cond ? "123" : std::string();, which is a reasonable trade-off.

Fixes #120206

@hokein hokein requested a review from Xazax-hun December 17, 2024 13:30
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2024

@llvm/pr-subscribers-clang

Author: Haojian Wu (hokein)

Changes

Fixes #120206


Full diff: https://github.com/llvm/llvm-project/pull/120233.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/CheckExprLifetime.cpp (+9)
  • (modified) clang/test/Sema/warn-lifetime-analysis-nocfg.cpp (+29)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index befa411e882b4c..2fb14da05bbef5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -677,6 +677,8 @@ Improvements to Clang's diagnostics
       views.push_back(std::string("123")); // warning
     }
 
+- Fix -Wdangling false positives on conditional operators (#120206).
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index 843fdb4a65cd73..e4eb7d66575bb9 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -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;
+    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
diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
index 45b4dc838f44ed..4c19367bb7f3dd 100644
--- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -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

// 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.

@hstk30-hw hstk30-hw self-requested a review December 18, 2024 03:43
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

Please add more details to the summary, especially a note that there theoretically could be false negatives but it fixes the overall issue and the tradeoff good.

@hokein
Copy link
Collaborator Author

hokein commented Dec 20, 2024

Please add more details to the summary, especially a note that there theoretically could be false negatives but it fixes the overall issue and the tradeoff good.

Done.

@hokein hokein merged commit a6d26c5 into llvm:main Dec 20, 2024
9 checks passed
@hokein hokein deleted the return-fp branch December 20, 2024 08:26
hokein added a commit that referenced this pull request Jan 22, 2025
…inter (#122088)

We currently have ad-hoc filtering logic for temporary object member
access in `VisitGSLPointerArg`. This logic filters out more cases than
it should, leading to false negatives. Furthermore, this location lacks
sufficient context to implement a more accurate solution.

This patch refines the filtering logic by moving it to the central
filtering location, `analyzePathForGSLPointer`, consolidating the logic
and avoiding scattered filtering across multiple places. As a result,
the special handling for conditional operators (#120233) is no longer
necessary.

This change also resolves #120543.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

-Wreturn-stack-address false positive
4 participants