Clang can not detect runtime error of reference binding to null pointer
| Bugzilla Link | 46089 |
| Version | trunk |
| OS | Linux |
| CC | @dwblaikie,@DougGregor,@zygoloid |
Extended Description
This simple case test.cc
#include<iostream>
int main(){
int &x = *(int *)nullptr; // Error: null reference
std::cout << "ok" << std::endl;
return 0;
}
In clang-trunk, clang-10, clang-9 can not detect runtime error, while clang-8 and lower version does.
$clang++-trunk -fsanitize=null test.cc ; ./a.out
test.cc:4:14: warning: binding dereferenced null pointer to reference has undefined behavior [-Wnull-dereference]
int &x = *(int *)nullptr;
^~~~~~~~~~~~~~~
1 warning generated.
ok
$clang++-8 -w -fsanitize=null test.cc ; ./a.out
test.cc:4:14: warning: binding dereferenced null pointer to reference has undefined behavior [-Wnull-dereference]
int &x = *(int *)nullptr;
^~~~~~~~~~~~~~~
1 warning generated.
test.cc:4:14: runtime error: reference binding to null pointer of type 'int'
ok
My clang-trunk version is
$ clang++-trunk --version
clang version 11.0.0 (https://github.com/llvm/llvm-project 210616bd38d589020b45f8cbbf9f9ef1296f2729)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/haoxin/compilers/llvm-clang/llvm-project/build/bin
Is clang delete runtime error message when -fsanitize=null is used in new recent versions after clang-8? If not, I think there something wrong with the above case.
We don't produce sanitizer warnings for code that we constant-fold away. In particular, when a reference is initialized by an expression we can constant-fold, we should produce a -Wnull-dereference warning, but we will not trigger a -fsanitize=null diagnostic because we don't emit any code to initialize the reference.
I think this is probably OK, so long as we do reliably produce the -Wnull-dereference warning for all variables whose initializers we constant-fold. But that's not the case currently. Consider:
int *const p = nullptr;
int &r = *p;
We constant-fold r's initializer, but don't produce any "null reference" warning here because we would need to look through an int *const variable's initializer to see the null (which we happen to not do as part of this check).
I suppose as an alternative, we could prevent the constant evaluator from ever constant-folding an operation that would create a null reference.
So sorry, it's a mistake by me.
in clang++-8 is like this
$clang++-8 -fsanitize=null test.cc ; ./a.out
test.cc:4:14: warning: binding dereferenced null pointer to reference has undefined behavior [-Wnull-dereference]
int &x = *(int *)nullptr;
^~~~~~~~~~~~~~~
1 warning generated.
test.cc:4:14: runtime error: reference binding to null pointer of type 'int'
ok
Oh, ignore both my comments - I see, you're talking about the runtime error, not the warning... (though the fact that clang prints the warning with -w sounds like a bug to me... )
Oh, sorry, I see - you used -w which turns off warnings.
I guess perhaps there was something broken in -w that didn't turn off the warning in old versions. It looks right/proper/correct that -w turns off this warning, like any other.
Seems to work for me at fca76b79456c916fd2ce193ef76d6e795bd9c105
#include<iostream>
int main(){
int &x = *(int *)nullptr; // Error: null reference
std::cout << "ok" << std::endl;
return 0;
}
$ clang++-tot -fsanitize=null null.cpp
null.cpp:4:14: warning: binding dereferenced null pointer to reference has undefined behavior [-Wnull-dereference]
int &x = *(int *)nullptr; // Error: null reference
^~~~~~~~~~~~~~~
1 warning generated.