[clang] [clang][analyzer]Add C++ polymorphic ptr arithmetic checker (PR #82977)

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 27 19:09:47 PST 2024


haoNoQ wrote:

Hi! I wonder if path-sensitive analysis is useful here at all. If you simply warn every time you see arithmetic performed on a pointer to a class with at least one virtual method, wouldn't that be quite accurate most of the time?

At a glance, such operation only makes sense when all subclasses are actually of the same size. I'm aware of pretty much exactly one real-world class of this kind: static analyzer's own `SVal` lol. You can detect this case by looking at one or two derived classes in the AST, to see if they add any new member variables. This won't work if base classes aren't visible in the AST, but your checker will also not work in this case(?)

I can also think of cases where path sensitivity is useful because it can catch cases like
```
Object *Objs;
char *x = reinterpret_cast<int *>(Objs);
x += 1;
```
but these aren't exactly the cases we're trying to catch; it's probably easier to think of them as a different problem that we can catch separately (invalid reinterpretation of a pointer).

So, maybe your checker should be a plain old compiler warning? Compiler warnings are great because they're much easier to enable, so they benefit a much larger portion of users. An on-by-default compiler warning will be even more accessible, and this one can probably be enabled by default? We usually prefer implementing checks as compiler warnings when they don't need sophisticated analysis (eg. path-sensitivity) or any sort of domain-specific knowledge.

https://github.com/llvm/llvm-project/pull/82977


More information about the cfe-commits mailing list