[hackers] [scc] Free parameter symbols after applying a type operator || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Wed, 12 Aug 2015 17:43:18 +0200 (CEST)

commit 3183b71d83817fd0ad0566889a941b69844e8451
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Wed Aug 12 12:47:58 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Wed Aug 12 13:19:03 2015 +0200

    Free parameter symbols after applying a type operator
    
    After applying a type operator in not possible to use these
    parameters anymore, so it is a good place for discarding them.

diff --git a/cc1/decl.c b/cc1/decl.c
index 991a784..10cc48c 100644
--- a/cc1/decl.c
+++ b/cc1/decl.c
_AT_@ -118,14 +118,12 @@ fundcl(struct dcldata *dp)
         Type type = {.n = {.elem = -1}, .pars = NULL};
         Symbol *syms[NR_FUNPARAM], **sp;
         size_t size;
- void *p;
+ void *pars = NULL;
 
         pushctx();
         expect('(');
 
- if (accept(')')) {
- dp = queue(dp, FTN, type.n.elem, type.pars);
- } else {
+ if (!accept(')')) {
                 type.n.elem = 0;
                 sp = syms;
                 do
_AT_@ -134,13 +132,13 @@ fundcl(struct dcldata *dp)
 
                 expect(')');
 
- dp = queue(dp, FTN, type.n.elem, type.pars);
                 if (type.n.elem != -1) {
                         size = type.n.elem * sizeof(Symbol *);
- p = memcpy(xmalloc(size), syms, size);
- dp = queue(dp, PARS, 0, p);
+ pars = memcpy(xmalloc(size), syms, size);
                 }
         }
+ dp = queue(dp, PARS, 0, pars);
+ dp = queue(dp, FTN, type.n.elem, type.pars);
 
         return dp;
 }
_AT_@ -210,6 +208,15 @@ declarator(Type *tp, unsigned ns, Type **otp)
                         pars = bp->data;
                         break;
                 default:
+ if (pars) {
+ /*
+ * constructor applied to a function. We don't
+ * need the parameter symbols anymore.
+ */
+ free(pars);
+ popctx();
+ pars = NULL;
+ }
                         tp = mktype(tp, bp->op, bp->nelem, bp->data);
                         break;
                 }
_AT_@ -217,7 +224,8 @@ declarator(Type *tp, unsigned ns, Type **otp)
         /*
          * FIXME: This assignation can destroy pars of a previous definition
          */
- sym->u.pars = pars;
+ if (pars)
+ sym->u.pars = pars;
         *otp = tp;
         return sym;
 }
_AT_@ -655,12 +663,14 @@ decl(void)
                         sym->flags &= ~ISEMITTED;
                         curfun = sym;
                         emit(OFUN, sym);
+ free(sym->u.pars);
                         compound(NULL, NULL, NULL);
                         emit(OEFUN, NULL);
                         curfun = NULL;
                         return;
                 default:
                 remove_pars:
+ free(sym->u.pars);
                         popctx();
                 }
         }
diff --git a/cc1/symbol.c b/cc1/symbol.c
index 2e36ded..1f5871d 100644
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
_AT_@ -130,7 +130,7 @@ popctx(void)
                                 warn("'%s' defined but not used", sym->name);
                 }
                 free(sym->name);
- // TODO: Fix this memory leak free(sym->u.pars);
+ // TODO: There is a memory leak with sym->u.s
                 free(sym);
         }
         head = sym;
Received on Wed Aug 12 2015 - 17:43:18 CEST

This archive was generated by hypermail 2.3.0 : Wed Aug 12 2015 - 17:48:13 CEST