tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit 5cff3c79f62476122a11b9a87489e3e9ee4f6222
parent 798876f385e18b1b0a750092319bd98ac0819179
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Tue, 20 Mar 2018 19:18:41 -0700

Fix crashes in eval_pair()

size was initialized at 0 so when it was doubled to grow the new list it
tried to reallocate the memory to 0 bytes. The realloc was also trying
to change the passed the argument not the new list. Additionally cap and
size were reversed adding addition confusing. Who wrote this shit, oh
wait.

Diffstat:
tisp.c | 12++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/tisp.c b/tisp.c @@ -389,17 +389,17 @@ tisp_read(char *cmd) static Val eval_pair(Hash env, Val v) { - int cap = 0, size = 0; + int cap = 1, size = 0; Val *new = emalloc(sizeof(Val)); while (!nilp(v)) { - new[cap++] = tisp_eval(env, car(v)); - if (cap == size) { - size *= 2; - v = erealloc(v, size*sizeof(Val)); + new[size++] = tisp_eval(env, car(v)); + if (size == cap) { + cap *= 2; + new = erealloc(new, cap*sizeof(Val)); } v = cdr(v); } - Val ret = mk_list(cap, new); + Val ret = mk_list(size, new); free(new); return ret; }