public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105569] New: -Waddress warns on dynamic_cast
@ 2022-05-11 15:47 anton.reinhard at protonmail dot com
2022-05-11 15:54 ` [Bug c++/105569] [12/13 Regression] " mpolacek at gcc dot gnu.org
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: anton.reinhard at protonmail dot com @ 2022-05-11 15:47 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105569
Bug ID: 105569
Summary: -Waddress warns on dynamic_cast
Product: gcc
Version: 12.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: anton.reinhard at protonmail dot com
Target Milestone: ---
compiling with -Waddress yields a useless warning compiling the following code:
Compiler version:
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-bootstrap
--prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-linker-build-id --enable-lto
--enable-multilib --enable-plugin --enable-shared --enable-threads=posix
--disable-libssp --disable-libstdcxx-pch --disable-werror
--with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (GCC)
Compiled using:
$ g++ warn.cpp -Waddress
warn.cpp: In function ‘int main()’:
warn.cpp:12:22: warning: comparing the result of pointer addition ‘(((A*)ref) +
((sizetype)(*(long int*)((& ref)->B::_vptr.B + -24))))’ and NULL [-Waddress]
12 | bool b = nullptr == dynamic_cast<A*>(&ref);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
The file warn.cpp:
class A {};
class B : public virtual A {};
class C : public A {};
int main() {
B* object = new B();
B &ref = *object;
// -Waddress warns here
bool b = nullptr == dynamic_cast<A*>(&ref);
// -Waddress doesn't warn anymore
auto ptr = dynamic_cast<A*>(&ref);
bool b2 = ptr == nullptr;
C* cobject = new C();
C &cref = *cobject;
// -Waddress also doesn't warn anymore
bool b3 = nullptr == dynamic_cast<A*>(&cref);
}
The warning:
warn.cpp: In function ‘int main()’:
warn.cpp:12:22: warning: comparing the result of pointer addition ‘(((A*)ref) +
((sizetype)(*(long int*)((& ref)->B::_vptr.B + -24))))’ and NULL [-Waddress]
12 | bool b = nullptr == dynamic_cast<A*>(&ref);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
However, dynamic_cast should return nullptr if the cast failed, so this warning
seems invalid to me. I don't know if just the warning is in error or if the
produced code is buggy.
It only happens when the derived class inherits with virtual (see class C)
It doesn't warn in versions previous to gcc-12, or in any clang versions I've
tried which makes me think it's a bug and not me making a mistake.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/105569] [12/13 Regression] -Waddress warns on dynamic_cast
2022-05-11 15:47 [Bug c++/105569] New: -Waddress warns on dynamic_cast anton.reinhard at protonmail dot com
@ 2022-05-11 15:54 ` mpolacek at gcc dot gnu.org
2022-05-11 17:08 ` mpolacek at gcc dot gnu.org
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-05-11 15:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105569
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Summary|-Waddress warns on |[12/13 Regression]
|dynamic_cast |-Waddress warns on
| |dynamic_cast
CC| |mpolacek at gcc dot gnu.org,
| |msebor at gcc dot gnu.org
Last reconfirmed| |2022-05-11
Ever confirmed|0 |1
Priority|P3 |P2
Keywords| |diagnostic
Target Milestone|--- |12.2
--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Started with r12-4059-g4dc7ce6fb39179. Maybe all we need is a
warning_sentinel.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/105569] [12/13 Regression] -Waddress warns on dynamic_cast
2022-05-11 15:47 [Bug c++/105569] New: -Waddress warns on dynamic_cast anton.reinhard at protonmail dot com
2022-05-11 15:54 ` [Bug c++/105569] [12/13 Regression] " mpolacek at gcc dot gnu.org
@ 2022-05-11 17:08 ` mpolacek at gcc dot gnu.org
2022-05-11 18:03 ` mpolacek at gcc dot gnu.org
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-05-11 17:08 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105569
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |ASSIGNED
Assignee|unassigned at gcc dot gnu.org |mpolacek at gcc dot gnu.org
--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Candidate fix:
--- a/gcc/cp/rtti.cc
+++ b/gcc/cp/rtti.cc
@@ -844,6 +844,9 @@ build_dynamic_cast (location_t loc, tree type, tree expr,
if (r != error_mark_node)
maybe_warn_about_useless_cast (loc, type, expr, complain);
protected_set_expr_location (r, loc);
+ tree sr = r;
+ STRIP_TYPE_NOPS (sr);
+ suppress_warning (sr, OPT_Waddress);
return r;
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/105569] [12/13 Regression] -Waddress warns on dynamic_cast
2022-05-11 15:47 [Bug c++/105569] New: -Waddress warns on dynamic_cast anton.reinhard at protonmail dot com
2022-05-11 15:54 ` [Bug c++/105569] [12/13 Regression] " mpolacek at gcc dot gnu.org
2022-05-11 17:08 ` mpolacek at gcc dot gnu.org
@ 2022-05-11 18:03 ` mpolacek at gcc dot gnu.org
2022-05-26 21:11 ` cvs-commit at gcc dot gnu.org
2022-05-26 21:13 ` mpolacek at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-05-11 18:03 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105569
--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Actually this is probably a better fix; I don't suppose we want to issue
warnings for the compiler-generated POINTER_PLUS_EXPRs.
--- a/gcc/cp/class.cc
+++ b/gcc/cp/class.cc
@@ -518,6 +518,8 @@ build_base_path (enum tree_code code,
expr = build1 (NOP_EXPR, ptr_target_type, expr);
+ suppress_warning (expr, OPT_Waddress);
+
indout:
if (!want_pointer)
{
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/105569] [12/13 Regression] -Waddress warns on dynamic_cast
2022-05-11 15:47 [Bug c++/105569] New: -Waddress warns on dynamic_cast anton.reinhard at protonmail dot com
` (2 preceding siblings ...)
2022-05-11 18:03 ` mpolacek at gcc dot gnu.org
@ 2022-05-26 21:11 ` cvs-commit at gcc dot gnu.org
2022-05-26 21:13 ` mpolacek at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-26 21:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105569
--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:
https://gcc.gnu.org/g:6f56efa94e845db0d5c934ca202295019bf334c1
commit r13-784-g6f56efa94e845db0d5c934ca202295019bf334c1
Author: Marek Polacek <polacek@redhat.com>
Date: Wed May 11 14:38:49 2022 -0400
c++: improve -Waddress warnings with *_cast [PR105569]
This patch improves the diagnostic for -Waddress when it warns for
if (dynamic_cast<A*>(&ref))
// ...
where 'ref' is a reference, which cannot be null. In particular, it
changes
warning: comparing the result of pointer addition '(((A*)ref) +
((sizetype)(*(long int*)((& ref)->B::_vptr.B + -24))))' and NULL
to
warning: the compiler can assume that the address of 'ref' will never be
NULL
PR c++/105569
gcc/cp/ChangeLog:
* typeck.cc (warn_for_null_address): Improve the warning when
the POINTER_PLUS_EXPR's base is of reference type.
gcc/testsuite/ChangeLog:
* g++.dg/warn/Waddress-9.C: New test.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/105569] [12/13 Regression] -Waddress warns on dynamic_cast
2022-05-11 15:47 [Bug c++/105569] New: -Waddress warns on dynamic_cast anton.reinhard at protonmail dot com
` (3 preceding siblings ...)
2022-05-26 21:11 ` cvs-commit at gcc dot gnu.org
@ 2022-05-26 21:13 ` mpolacek at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-05-26 21:13 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105569
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|ASSIGNED |RESOLVED
Resolution|--- |FIXED
--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
The warning is correct, but I've improved the diagnostic at least. Not
planning to backport such an improvement.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-05-26 21:13 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-11 15:47 [Bug c++/105569] New: -Waddress warns on dynamic_cast anton.reinhard at protonmail dot com
2022-05-11 15:54 ` [Bug c++/105569] [12/13 Regression] " mpolacek at gcc dot gnu.org
2022-05-11 17:08 ` mpolacek at gcc dot gnu.org
2022-05-11 18:03 ` mpolacek at gcc dot gnu.org
2022-05-26 21:11 ` cvs-commit at gcc dot gnu.org
2022-05-26 21:13 ` mpolacek at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).