[clang] [Clang][CodeGen] Do not set inbounds flag for struct GEP with null base pointers (PR #130734)

Yingwei Zheng via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 13 01:53:55 PDT 2025


dtcxzyw wrote:

> this check needs to happen at a higher level. And at that level, you should have an Expr*, not just an Address.

I tried to use `Expr::isNullPointerConstant` in `CodeGenFunction::EmitMemberExpr`:
https://github.com/llvm/llvm-project/blob/59fd2878fce470f02058f43d4b818ed68ce04094/clang/lib/CodeGen/CGExpr.cpp#L4774-L4781
```
struct S {
  int x, y;
};

using uintptr_t = unsigned long long;
uintptr_t get_offset_of_y_naively() {
  return ((uintptr_t)(&(((S *)nullptr)->y)));
}
```
The base expr is:
```
ParenExpr 0x58d207ad2048 'S *'
`-CStyleCastExpr 0x58d207ad2020 'S *' <NoOp>
  `-ImplicitCastExpr 0x58d207ad2008 'S *' <NullToPointer> part_of_explicit_cast
    `-CXXNullPtrLiteralExpr 0x58d207ad1f80 'nullptr_t'
```
`isNullPointerConstant` returns false regardless of the value of NullPointerConstantValueDependence.

> Do you care at all about patterns like ((uintptr_t)(&(((S *)nullptr)->y.z)));?

Yes. This pattern is used by openssl.


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


More information about the cfe-commits mailing list