commit cae714c448ebdea4a6de048b012927b96be9b628
parent c1768b2e370906fc8418bc26cd69d88529c8c96b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 14 Apr 2014 10:38:09 +0200
Check against assigns to const variables
It is implementation defined what happens in that cases, and
I choose report an error.
Diffstat:
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/expr.c b/expr.c
@@ -511,9 +511,10 @@ ternary(void)
static Node *
assign(void)
{
- register Node *np1 = ternary(), *np2;
+ register Node *np1, *np2;
char *err;
+ np1 = ternary();
for (;;) {
register char op;
@@ -535,6 +536,8 @@ assign(void)
np2 = assign();
if (!np1->b.lvalue)
goto nolvalue;
+ if (isconst(np1->type->op))
+ goto const_mod;
/* TODO: if it necessary a 0 comparision? */
if ((np2 = convert(np2, np1->type)) == NULL)
goto incompatibles;
@@ -543,6 +546,9 @@ assign(void)
return_np:
return np1;
+const_mod:
+ err = "const value modified";
+ goto error;
nolvalue:
err = "lvalue required as left operand of assignment";
goto error;
diff --git a/types.c b/types.c
@@ -144,7 +144,7 @@ mktype(Type *tp, uint8_t op,
case ARY: letter = 'V'; break;
case ENUM: letter = 'E'; break;
case STRUCT: letter = 'S'; break;
- default: abort();
+ default: letter = tp->letter;
}
bp = xmalloc(sizeof(*bp));
bp->next = *tbl;