This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ PATCH: PR 31449


This patch fixes PR c++/31449, whereby we were silently allowing
const-incorrect assignments.  The proximate cause was that
"static_cast<const T*>" ended up with type "T*" (missing the "const").
The root problem was that build_base_path takes a binfo for the
destination type, and binfos are always cv-unqualified.  We converted
to the binfo type, and thereby lost the cv-qualification.  We needed
to preserve the cv-qualification of the input.

Thanks to Volker for identifying which earlier patch caused this
problem. 

Tested on x86_64-unknown-linux-gnu, committed to mainline.  I will
backport to other branches shortly.

--
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713

2007-04-09  Mark Mitchell  <mark@codesourcery.com>

	PR c++/31449
	* class.c (build_base_path): Ensure that the converted pointer has
	the same cv-qualification as the input.

2007-04-09  Mark Mitchell  <mark@codesourcery.com>

	PR c++/31449
	* g++.dg/init/const5.C: New test.

Index: gcc/cp/class.c
===================================================================
--- gcc/cp/class.c	(revision 123672)
+++ gcc/cp/class.c	(working copy)
@@ -303,7 +303,18 @@ build_base_path (enum tree_code code,
 	 field, because other parts of the compiler know that such
 	 expressions are always non-NULL.  */
       if (!virtual_access && integer_zerop (offset))
-	return build_nop (build_pointer_type (target_type), expr);
+	{
+	  tree class_type;
+	  /* TARGET_TYPE has been extracted from BINFO, and, is
+	     therefore always cv-unqualified.  Extract the
+	     cv-qualifiers from EXPR so that the expression returned
+	     matches the input.  */
+	  class_type = TREE_TYPE (TREE_TYPE (expr));
+	  target_type
+	    = cp_build_qualified_type (target_type,
+				       cp_type_quals (class_type));
+	  return build_nop (build_pointer_type (target_type), expr);
+	}
       null_test = error_mark_node;
     }
 
Index: gcc/testsuite/g++.dg/init/const5.C
===================================================================
--- gcc/testsuite/g++.dg/init/const5.C	(revision 0)
+++ gcc/testsuite/g++.dg/init/const5.C	(revision 0)
@@ -0,0 +1,11 @@
+// PR c++/31449
+
+class Foo {};
+class Bar : public Foo {};
+static const Foo *foo = 0;
+
+static Bar *bar = static_cast<const Bar*>(foo); // { dg-error "conversion" }
+
+void func(const Foo *foo) {
+  Bar *bar = static_cast<const Bar*>(foo);  // { dg-error "conversion" }
+}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]