diff mbox

[C] Don't accept a cast as a valid LHS (PR c/66341)

Message ID 20150603173222.GJ2756@redhat.com
State New
Headers show

Commit Message

Marek Polacek June 3, 2015, 5:32 p.m. UTC
Since a cast does not yield an lvalue, we shouldn't accept invalid
code as in the attached testcase.

The problem was that we weren't setting NON_LVALUE_EXPR properly;
for comparing expressions we shouldn't use ==, because then a cast
in an expression might confuse us.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-06-03  Marek Polacek  <polacek@redhat.com>

	PR c/66341
	* c-typeck.c (build_c_cast): Use c_tree_equal rather than a plain
	comparison.

	* gcc.dg/lvalue-8.c: New test.


	Marek

Comments

Joseph Myers June 3, 2015, 6:02 p.m. UTC | #1
On Wed, 3 Jun 2015, Marek Polacek wrote:

> Since a cast does not yield an lvalue, we shouldn't accept invalid
> code as in the attached testcase.
> 
> The problem was that we weren't setting NON_LVALUE_EXPR properly;
> for comparing expressions we shouldn't use ==, because then a cast
> in an expression might confuse us.

I'm not clear on what trees you get in the problem case (or which of the 
tests in the testcase you added was broken before - they don't seem that 
similar to the tests in the bug).  It would seem safer to me to use 
lvalue_p (value) as the condition for adding a NON_VALUE_EXPR.

> +  /* A cast to a qualified type has the same effect as a cast
> +     to the unqualified version of the type.  */
> +  (const int *) p = 0; /* { dg-error "lvalue required as left operand of assignment" } */

That's not a cast to a qualified type.  A cast to qualified type would be 
(int *const) p.
diff mbox

Patch

diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index f55d4c6..f13251b 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -5195,7 +5195,7 @@  build_c_cast (location_t loc, tree type, tree expr)
     }
 
   /* Don't let a cast be an lvalue.  */
-  if (value == expr)
+  if (c_tree_equal (value, expr))
     value = non_lvalue_loc (loc, value);
 
   /* Don't allow the results of casting to floating-point or complex
diff --git gcc/testsuite/gcc.dg/lvalue-8.c gcc/testsuite/gcc.dg/lvalue-8.c
index e69de29..085ce95 100644
--- gcc/testsuite/gcc.dg/lvalue-8.c
+++ gcc/testsuite/gcc.dg/lvalue-8.c
@@ -0,0 +1,19 @@ 
+/* PR c/66341 */
+/* { dg-do compile } */
+
+void
+foo (int *p)
+{
+  p = 0;
+  /* A cast does not yield an lvalue.  */
+  (int *) p = 0; /* { dg-error "lvalue required as left operand of assignment" } */
+  /* A cast to a qualified type has the same effect as a cast
+     to the unqualified version of the type.  */
+  (const int *) p = 0; /* { dg-error "lvalue required as left operand of assignment" } */
+  (int *) (char *) p = 0; /* { dg-error "lvalue required as left operand of assignment" } */
+  (int *) (char *) (int *) p = 0; /* { dg-error "lvalue required as left operand of assignment" } */
+  (int *) (char *) (int *) (char *) p = 0; /* { dg-error "lvalue required as left operand of assignment" } */
+  (int *) (double *) p = 0; /* { dg-error "lvalue required as left operand of assignment" } */
+  (int *) (int *) p = 0; /* { dg-error "lvalue required as left operand of assignment" } */
+  (int *) (const int *) p = 0; /* { dg-error "lvalue required as left operand of assignment" } */
+}