[C PATCH] Don't accept a cast as a valid LHS (PR c/66341)
Marek Polacek
polacek@redhat.com
Wed Jun 3 19:29:00 GMT 2015
On Wed, Jun 03, 2015 at 06:02:12PM +0000, Joseph Myers wrote:
> 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.
E.g.
int *p;
(int *) (char *) p = 0;
wasn't rejected before. In this case build_c_cast tried to cast
"(char *) p", which is a NOP_EXPR, to "(int *)". There's this
value = convert (type, value);
which trims the NOP_EXPR and we get "p". Then there's
if (value == expr)
value = non_lvalue_loc (loc, value);
but that's false since it's comparing "p" and "(char *) p", so we don't
wrap the "p" in NON_VALUE_EXPR. Hopefully this makes the issue a tad
clearer.
Using lvalue_p is fine as well, so adjusted.
> That's not a cast to a qualified type. A cast to qualified type would be
> (int *const) p.
Duh, adjusted. Thanks.
Bootstrapped/regtested on x86_64-linux pending, ok if it passes?
2015-06-03 Marek Polacek <polacek@redhat.com>
PR c/66341
* c-typeck.c (build_c_cast): Wrap VALUE into NON_LVALUE_EXPR if
it is a lvalue.
* gcc.dg/lvalue-8.c: New test.
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index f55d4c6..6b313f3 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 (lvalue_p (value))
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..04eeb71 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. */
+ (int *const) 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 *) (int *const) p = 0; /* { dg-error "lvalue required as left operand of assignment" } */
+}
Marek
More information about the Gcc-patches
mailing list