[PATCH] D64256: Teach some warnings to respect gsl::Pointer and gsl::Owner attributes

Gábor Horváth via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 12 17:09:28 PDT 2019


xazax.hun added a comment.

In D64256#1626279 <https://reviews.llvm.org/D64256#1626279>, @leonardchan wrote:

> In D64256#1626025 <https://reviews.llvm.org/D64256#1626025>, @xazax.hun wrote:
>
> > In D64256#1625998 <https://reviews.llvm.org/D64256#1625998>, @leonardchan wrote:
> >
> > > Hi. I noticed in our builders that both of the warnings introduced in this patch are being diagnosed for pointers that don't use GSL at all. I'm attempting to make a small reproducer now.
> >
> >
> > Hi!
> >
> > Clang now apply GSL annotations for some STL types automatically. So this is the expected behavior. Are any of those warnings unwanted/false positive with the newest Clang version? If so, please share the reproducers and I will either fix them ASAP or revert/turn off the warnings.
>
>
> Thanks! I didn't know that they were applied to stl types. This could explain why the warning is raised in my case (std::string), but I don't think there's anything wrong in this example that would lead to a warning:
>
>   leonardchan at cp-snakewater:~/llvm-monorepo/llvm-build-3-master$ cat ~/misc/test.cpp
>   #include <string>
>  
>   std::string MakeStr();
>   void other_func(const char *s);
>  
>   void func(std::string s) {
>     const char *x = MakeStr().c_str();
>     other_func(x);
>   }
>   leonardchan at cp-snakewater:~/llvm-monorepo/llvm-build-3-master$ ~/llvm-monorepo/llvm-build-3-master/bin/clang++ -c ~/misc/test.cpp
>   /usr/local/google/home/leonardchan/misc/test.cpp:7:19: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling]
>     const char *x = MakeStr().c_str();
>                     ^~~~~~~~~
>   1 warning generated.
>
>
> This was made using a release build from ToT clang.


Thanks for the repro! I think this is a true positive. `MakeStr`  will create a temporary object, and `c_str()` will create a pointer that points into the buffer owned by the temporary object. At the end of the full expression the temporary object is destroyed and the result of `c_str` will dangle.


Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D64256/new/

https://reviews.llvm.org/D64256





More information about the cfe-commits mailing list