Skip to content

Missing dangling warnings for normal local array returned as std::span #100567

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

Closed
hokein opened this issue Jul 25, 2024 · 2 comments · Fixed by #103716
Closed

Missing dangling warnings for normal local array returned as std::span #100567

hokein opened this issue Jul 25, 2024 · 2 comments · Fixed by #103716
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party

Comments

@hokein
Copy link
Collaborator

hokein commented Jul 25, 2024

See the example: https://godbolt.org/z/nnjKszT4c

I think the reason is that we don't know the type abc2 is gsl-owner.

#include <span>
#include <vector>

using namespace std;

span<const int> func1() {
  int abc2[10];
  return abc2; // bad, no dangling warning
}

span<const int> func2() {
  std::vector<int> abc;
  return abc; // good, danging warning.
}
@hokein hokein added clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer labels Jul 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 25, 2024

@llvm/issue-subscribers-clang-frontend

Author: Haojian Wu (hokein)

See the example: https://godbolt.org/z/nnjKszT4c

I think the reason is that we don't know the type abc2 is gsl-owner.

#include &lt;span&gt;
#include &lt;vector&gt;

using namespace std;

span&lt;const int&gt; func1() {
  int abc2[10];
  return abc2; // bad, no dangling warning
}

span&lt;const int&gt; func2() {
  std::vector&lt;int&gt; abc;
  return abc; // good, danging warning.
}

@hokein
Copy link
Collaborator Author

hokein commented Aug 13, 2024

Another example is:

std::string_view TestZoneName() {
   char test[] = "foo";
   return test;
}

@shafik shafik added the confirmed Verified by a second party label Aug 20, 2024
hokein added a commit that referenced this issue Aug 28, 2024
…tor (#103716)

With this patch, clang now automatically adds
``[[clang::lifetimebound]]`` to the parameters of `std::span,
std::string_view` constructors, this enables Clang to capture more cases
where the returned reference outlives the object.


Fixes #100567
@EugeneZelenko EugeneZelenko removed the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Aug 28, 2024
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" confirmed Verified by a second party
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants