Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent double-free when y is in array t #182

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ extern void arginit(int, char **);
extern void envinit(char **);
extern Array *makesymtab(int);
extern void freesymtab(Cell *);
extern int freesymtabcheck(Cell *, Cell*);
extern void freeelem(Cell *, const char *);
extern Cell *setsymtab(const char *, const char *, double, unsigned int, Array *);
extern int hash(const char *, int);
Expand Down
9 changes: 5 additions & 4 deletions run.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,15 @@ Cell *call(Node **a, int n) /* function call. very kludgy and fragile */
if (isarr(t)) {
if (t->csub == CCOPY) {
if (i >= ncall) {
freesymtab(t);
if (freesymtabcheck(t, y)) {
freed = 1;
}
t->csub = CTEMP;
tempfree(t);
} else {
oargs[i]->tval = t->tval;
oargs[i]->tval &= ~(STR|NUM|DONTFREE);
oargs[i]->sval = t->sval;
tempfree(t);
}
}
} else if (t != y) { /* kludge to prevent freeing twice */
Expand All @@ -313,9 +314,9 @@ Cell *call(Node **a, int n) /* function call. very kludgy and fragile */
}
}
tempfree(fcn);
if (isexit(y) || isnext(y))
return y;
if (freed == 0) {
if (isexit(y) || isnext(y))
return y;
tempfree(y); /* don't free twice! */
}
z = frp->retval; /* return value */
Expand Down
34 changes: 34 additions & 0 deletions tran.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,40 @@ void freesymtab(Cell *ap) /* free a symbol table */
free(tp);
}

int freesymtabcheck(Cell *ap, Cell *needle) /* free a symbol table, return 1 if needle was freed, 0 otherwise */
{
Cell *cp, *temp;
Array *tp;
int i, retval = 0;

DPRINTF("insymtab %p: n=%s s=\"%s\" f=%g t=%o\n",
(void*)ap, ap->nval, ap->sval, ap->fval, ap->tval);

if (!isarr(ap))
return 0;
tp = (Array *) ap->sval;
if (tp == NULL)
return 0;
for (i = 0; i < tp->size; i++) {
for (cp = tp->tab[i]; cp != NULL; cp = temp) {
if (cp == needle)
retval = 1;
xfree(cp->nval);
if (freeable(cp))
xfree(cp->sval);
temp = cp->cnext; /* avoids freeing then using */
free(cp);
tp->nelem--;
}
tp->tab[i] = NULL;
}
if (tp->nelem != 0)
WARNING("can't happen: inconsistent element count freeing %s", ap->nval);
free(tp->tab);
free(tp);
return retval;
}

void freeelem(Cell *ap, const char *s) /* free elem s from ap (i.e., ap["s"] */
{
Array *tp;
Expand Down