aboutsummaryrefslogtreecommitdiff
path: root/src/prc1/type.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2026-10-31 13:52:42 +0100
committerXavier Del Campo Romero <xavi92@disroot.org>2026-10-31 13:52:42 +0100
commitcc1cbe64a5953004c80b0da3026748ee3061bca0 (patch)
tree03c68b482be40479b2f73e0a63a79ffd3f39bcb1 /src/prc1/type.c
parent4dbcfc991a4fb203ec2b50d950f59adb1ad61922 (diff)
downloadprc-cc1cbe64a5953004c80b0da3026748ee3061bca0.tar.gz
Use read-only references for complex types
This fixes a double-free error in constructs such as: types mystruct struct dummy int end myalias alias pointer to mystruct
Diffstat (limited to 'src/prc1/type.c')
-rw-r--r--src/prc1/type.c33
1 files changed, 12 insertions, 21 deletions
diff --git a/src/prc1/type.c b/src/prc1/type.c
index cdd8f72..fbb332b 100644
--- a/src/prc1/type.c
+++ b/src/prc1/type.c
@@ -82,8 +82,8 @@ void type_lfree(struct type *t)
{
if (t)
{
- if (t->child)
- type_lfree(t->child);
+ if (t->child.rw && !t->readonly)
+ type_lfree(t->child.rw);
type_free(t);
}
@@ -154,7 +154,7 @@ char *type_name(const struct type *t)
return NULL;
}
- t = t->child;
+ t = t->child.ro;
}
return ret;
@@ -200,10 +200,10 @@ static void queue(struct prv *p, struct type *t)
p->t = t;
else
{
- while (tt->child)
- tt = tt->child;
+ while (tt->child.rw)
+ tt = tt->child.rw;
- tt->child = t;
+ tt->child.rw = t;
}
p->stk = p->tk;
@@ -343,7 +343,7 @@ static int aryeq(const struct type *a, const struct type *b)
static int eq(const struct type *a, const struct type *b)
{
- for (; a && b; a = a->child, b = b->child)
+ for (; a && b; a = a->child.ro, b = b->child.ro)
if (a->type != b->type)
return 0;
else if (a->type == ARY && !aryeq(a, b))
@@ -391,26 +391,17 @@ static int t(const struct lex *l, struct prv *p)
}
else if (tt)
{
- struct type *nt = malloc(sizeof *nt);
+ while (tt->child.rw)
+ tt = tt->child.rw;
- if (!nt)
- {
- perror("malloc(3)");
- return -1;
- }
-
- *nt = *t;
-
- while (tt->child)
- tt = tt->child;
-
- tt->child = nt;
+ tt->child.ro = t;
+ tt->readonly = 1;
tt = p->t;
/* array size still undecided */
if (tt->type == ARY && !tt->sz)
{
- tt->sz = tt->u.ary.n * nt->sz;
+ tt->sz = tt->u.ary.n * t->sz;
tt->align = t->align;
}