commit bbc13f1280dc042f22868c2565602f5b01c2873f
parent 010fd7349f3e3baeeea0344175cd351dd4692904
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Wed, 16 Oct 2024 10:31:15 +0200
cc1: Fix empty param list in newtype()
When a function does not have parameters (a void function)
then tp->n.elem is 0, and it was calling malloc with a size
of 0 and then later in eqfunc() we are accessing the pointer
returned by malloc(). It happens to work in linux, but
obviously, it was wrong.
Also, the malloc() call didn't allocate space for the NULL
pointer that must finish the list and it was expected by
eqfunc().
Diffstat:
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/src/cmd/cc/cc1/types.c b/src/cmd/cc/cc1/types.c
@@ -256,7 +256,7 @@ deftype(Type *tp)
static Type *
newtype(Type *base)
{
- Type *tp;
+ Type *tp, **pars;
size_t siz;
tp = xmalloc(sizeof(*tp));
@@ -264,8 +264,12 @@ newtype(Type *base)
tp->id = newid();
if (tp->op == FTN) {
- siz = tp->n.elem * sizeof(Type *);
- tp->p.pars = memcpy(xmalloc(siz), tp->p.pars, siz);
+ siz = (tp->n.elem + 1) * sizeof(Type *);
+ pars = xmalloc(siz);
+ if (tp->n.elem > 0)
+ memcpy(pars, tp->p.pars, siz);
+ pars[tp->n.elem] = NULL;
+ tp->p.pars = pars;
} else if (tp->op == ARY) {
/* We need alignment for flexible array members */
tp->align = tp->type->align;