Ammar James noticed that tcc does not properly protect const struct
types from access to members:
struct somestruct {
int i;
};
int main(void)
{
struct somestruct item;
const struct somestruct *cssp;
item.i = 1;
cssp = &item;
cssp->i = 0; /* not permitted - cssp points to const objects */
return 0;
}
tcc should issue a warning, but did not. gcc does give an error.
The reason is that tcc knows the struct type is const, and protects
the struct as a whole from assignment, but the struct members are not
individually marked as const, and it does not propogate the constness
from a struct to its members.
I found a way to fix it:
diff --git a/tccgen.c b/tccgen.c
index 942c503..5e0298a 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -3266,9 +3266,11 @@ static void unary(void)
inc(1, tok);
next();
} else if (tok == '.' || tok == TOK_ARROW) {
+ int is_const;
/* field */
if (tok == TOK_ARROW)
indir();
+ is_const = vtop->type.t & VT_CONSTANT;
test_lvalue();
gaddrof();
next();
@@ -3290,6 +3292,7 @@ static void unary(void)
gen_op('+');
/* change type to field type, and set to lvalue */
vtop->type = s->type;
+ vtop->type.t |= is_const;
/* an array is never an lvalue */
if (!(vtop->type.t & VT_ARRAY)) {
vtop->r |= lvalue_type(vtop->type.t);
I don't know if this is the best way to fix it, but it does cause tcc to
generate the warning.
regards,
Sam Watkins
_______________________________________________
Tinycc-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/tinycc-devel