[clang] [clang][analyzer] Improve PointerSubChecker (PR #96501)

Daniel Krupp via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 4 02:25:07 PDT 2024


dkrupp wrote:

> > Even protobuf contains this type of code: https://codechecker-demo.eastus.cloudapp.azure.com/Default/report-detail?run=protobuf_v3.13.0_pointersub1&is-unique=on&diff-type=New&checker-name=alpha.core.PointerSub&report-id=5545776&report-hash=1bcd310fbaeccbcc13645b9b277239a2&report-filepath=%2adescriptor.pb.cc
> 
> I still think that this (1) is undeniably undefined behavior (2) isn't common, so won't cause "spam" problems and (3( can be replaced by standard-compliant code (`offsetof`) so there is no need to introduce a special case for it.

I agree with @NagyDonat that we don't need special handling of this case in the code, however I think the checker [documentation ](https://clang.llvm.org/docs/analyzer/checkers.html#alpha-core-pointersub-c) should be extended with the description of this special case as it may be a surprising warning from the checker with an example. Specifically that it warns for cases where two pointers are subtracted which point to members of the same struct and suggest the usage of the standard compliant solution: offsetof.

So please describe which pointer subtractions the checker accepts and which don't (with examples) and a reference to the standard where it describes the undefined behaviour.


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


More information about the cfe-commits mailing list