diff options
author | Nathaniel Shead <nathanieloshead@gmail.com> | 2023-12-17 12:46:02 +1100 |
---|---|---|
committer | Nathaniel Shead <nathanieloshead@gmail.com> | 2023-12-20 08:18:56 +1100 |
commit | 5ba949c096f5250aa4efb94fb7c94d1304c1bf39 (patch) | |
tree | 9b29b2f3ac81eafcfdd9dfe5fd0d2a984350abfe /gcc/testsuite/g++.dg/cpp0x | |
parent | d31c54c7da7661a964de59765fa8e7938796cc86 (diff) | |
download | gcc-5ba949c096f5250aa4efb94fb7c94d1304c1bf39.zip gcc-5ba949c096f5250aa4efb94fb7c94d1304c1bf39.tar.gz gcc-5ba949c096f5250aa4efb94fb7c94d1304c1bf39.tar.bz2 |
c++: Check null pointer deref when calling memfn in constexpr [PR102420]
Calling a non-static member function on a null pointer is undefined
behaviour (see [expr.ref] p8) and should error in constant evaluation,
even if the 'this' pointer is never actually accessed within that
function.
One catch is that currently, the function pointer conversion operator
for lambdas passes a null pointer as the 'this' pointer to the
underlying 'operator()', so for now we ignore such calls.
PR c++/102420
gcc/cp/ChangeLog:
* constexpr.cc (cxx_bind_parameters_in_call): Check for calling
non-static member functions with a null pointer.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/constexpr-memfn2.C: New test.
Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Diffstat (limited to 'gcc/testsuite/g++.dg/cpp0x')
-rw-r--r-- | gcc/testsuite/g++.dg/cpp0x/constexpr-memfn2.C | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-memfn2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-memfn2.C new file mode 100644 index 0000000..dde9416 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-memfn2.C @@ -0,0 +1,10 @@ +// PR c++/102420 +// { dg-do compile { target c++11 } } + +struct X { + constexpr int f() { return 0; } +}; +constexpr int g(X* x) { + return x->f(); // { dg-error "dereferencing a null pointer" } +} +constexpr int t = g(nullptr); // { dg-message "in .constexpr. expansion" } |