[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] weird error message.

From: Sam Watkins
Subject: Re: [Tinycc-devel] weird error message.
Date: Tue, 16 Jun 2009 03:16:25 +0800
User-agent: Mutt/1.5.13 (2006-08-11)

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);
         } else if (tok == '.' || tok == TOK_ARROW) {
+            int is_const;
             /* field */ 
             if (tok == TOK_ARROW) 
+            is_const = vtop->type.t & VT_CONSTANT;
@@ -3290,6 +3292,7 @@ static void unary(void)
             /* 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.


Sam Watkins

reply via email to

[Prev in Thread] Current Thread [Next in Thread]