Open tbaederr opened 2 weeks ago
@llvm/issue-subscribers-clang-frontend
Author: Timm Baeder (tbaederr)
It should work in neither case. Member access is only defined when accessing a subobject of an object, and nullptr doesn't point to an object. Looks like we're missing a check in the handling of ->
.
Simply changing the check from IsNullPtr
to getLValueBase().isNull()
seems to do the trick.
Sorry for the clunky title.
Consider this code:
The output is:
We diagnose this here: https://github.com/llvm/llvm-project/blob/7fe149cdf09d04fb8390b97c91bd9214c968cd3e/clang/lib/AST/ExprConstant.cpp#L1715-L1720
Because
IsNullPtr
is true here: https://github.com/llvm/llvm-project/blob/7fe149cdf09d04fb8390b97c91bd9214c968cd3e/clang/lib/AST/ExprConstant.cpp#L1702-L1712However, if the field we're talking about coincidentally doesn't start at offset 0 (and thus
c->a
isn't0
), we don't diagnose at all and this example just works:https://godbolt.org/z/vzTr6sEMo
As you can see in the godbolt link, GCC rejects this as well.
I'm wondering that is right here. Should the code work in both cases or in neither case?
CC @zygoloid @AaronBallman