Dangling std::string_view checker

A few years ago I wrote a dangling std::string_view checker as part of my university coursework. I meant to upstream it, but couldn’t find the time after I started working full time. I am not super up-to-date with the latest Static Analyzer developments, but would be happy to draw up a proposal now. Is this something that would be useful / welcome?

Welcome back!

I’m not aware of any checkers in recent years that implements something like that, it would be more than welcome! Would it be a part of InnerPointerChecker, or something brand new?

There already exists the following: clang-tidy - bugprone-dangling-handle — Extra Clang Tools 20.0.0git documentation
I don’t know how it compares to your check

I wonder if force-inlineing the relevant member functions of std::string_view in conjunction with InnerPointerChecker could also work for catching dangling pointers. As @Szelethus suggested, I also wonder how would it fit with the existing InnerPointerChecker.

Roughly how large is your total diff?

Hi long time no see! You’re absolutely welcome to extend your original checker or split it up as much as you think is necessary.

The -Wdangling work around GSL-ish lifetime annotations is indeed quite successful. However, I think path sensitivity can still be very beneficial in this area. It probably makes a lot of sense to “marry” the two approaches by developing a general-purpose path-sensitive checker that responds to arbitrary lifetime annotations. But even if we ultimately want that, your code could be a great place to start.

1 Like