summaryrefslogtreecommitdiffstats
path: root/clang/test/Analysis/reference.cpp
diff options
context:
space:
mode:
authorJordan Rose <jordan_rose@apple.com>2012-08-04 00:25:30 +0000
committerJordan Rose <jordan_rose@apple.com>2012-08-04 00:25:30 +0000
commit4aa80e156d5e446eff242556bc0a159bdf8dc2a8 (patch)
treeeb1a66737acca1ee3dca4a59d2cae71978324e46 /clang/test/Analysis/reference.cpp
parent08af4c84618198679be67d18cbfda82547a07edc (diff)
downloadbcm5719-llvm-4aa80e156d5e446eff242556bc0a159bdf8dc2a8.tar.gz
bcm5719-llvm-4aa80e156d5e446eff242556bc0a159bdf8dc2a8.zip
[analyzer] Don't assume values bound to references are automatically non-null.
While there is no such thing as a "null reference" in the C++ standard, many implementations of references (including Clang's) do not actually check that the location bound to them is non-null. Thus unlike a regular null dereference, this will not cause a problem at runtime until the reference is actually used. In order to catch these cases, we need to not prune out paths on which the input pointer is null. llvm-svn: 161288
Diffstat (limited to 'clang/test/Analysis/reference.cpp')
-rw-r--r--clang/test/Analysis/reference.cpp15
1 files changed, 14 insertions, 1 deletions
diff --git a/clang/test/Analysis/reference.cpp b/clang/test/Analysis/reference.cpp
index c9bfadced79..06e4a50e44c 100644
--- a/clang/test/Analysis/reference.cpp
+++ b/clang/test/Analysis/reference.cpp
@@ -91,12 +91,25 @@ namespace PR13440 {
}
}
-void testRef() {
+void testNullReference() {
int *x = 0;
int &y = *x; // expected-warning{{Dereference of null pointer}}
y = 5;
}
+void testRetroactiveNullReference(int *x) {
+ // According to the C++ standard, there is no such thing as a
+ // "null reference". So the 'if' statement ought to be dead code.
+ // However, Clang (and other compilers) don't actually check that a pointer
+ // value is non-null in the implementation of references, so it is possible
+ // to produce a supposed "null reference" at runtime. The analyzer shoeuld
+ // still warn when it can prove such errors.
+ int &y = *x;
+ if (x != 0)
+ return;
+ y = 5; // expected-warning{{Dereference of null pointer}}
+}
+
// ------------------------------------
// False negatives
OpenPOWER on IntegriCloud