Skip to content

Commit 77574b2

Browse files
committed
Fix a memory management bug in list concatination noted is issue #30.
1 parent 32d3ce6 commit 77574b2

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/bbs-lisp-evaluate-symb.adb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ package body BBS.lisp.evaluate.symb is
264264
-- Concatinate lists
265265
--
266266
declare
267-
cons_head : cons_index := NIL_CONS;
268-
dest_cons : cons_index := NIL_CONS;
269-
temp_cons : cons_index := NIL_CONS;
270-
src_cons : cons_index := NIL_CONS;
267+
cons_head : cons_index := NIL_CONS; -- Head of list being built
268+
dest_cons : cons_index := NIL_CONS; -- Current element in list being built
269+
temp_cons : cons_index := NIL_CONS; -- New cons cell to add to list
270+
src_cons : cons_index := NIL_CONS; -- Source list to copy from
271271
begin
272272
if s1 = NIL_CONS then
273273
error("concatenate", "Cannot concatenate a single element.");
@@ -279,31 +279,40 @@ package body BBS.lisp.evaluate.symb is
279279
if t2.kind = V_ERROR then
280280
error("concatenate", "Error reported evaluating additional parameters.");
281281
e := t2;
282+
BBS.lisp.conses.deref(cons_head);
282283
return;
283284
end if;
284285
src_cons := getList(t2);
285286
if src_cons = NIL_CONS then
286287
error("concatenate", "Parameter does not evaluate to a list");
287288
BBS.lisp.memory.deref(t2);
289+
BBS.lisp.conses.deref(cons_head);
288290
e := make_error(ERR_WRONGTYPE);
289291
return;
290292
end if;
291293
loop
292294
if not BBS.lisp.conses.alloc(temp_cons) then
293295
error("concatenate", "Unable to allocate cons cell.");
294296
BBS.lisp.conses.deref(cons_head);
297+
BBS.lisp.conses.deref(src_cons);
295298
e := make_error(ERR_ALLOCCONS);
296299
return;
297300
end if;
298301
if cons_head = NIL_CONS then
299302
cons_head := temp_cons;
300303
dest_cons := temp_cons;
301304
else
305+
--
306+
-- Point end of list to new cons cell and update end of list.
307+
--
302308
BBS.lisp.conses.set_cdr(dest_cons, (kind => V_LIST, l => temp_cons));
303309
dest_cons := temp_cons;
304310
end if;
311+
--
312+
-- Copy the value from the source list and move to the next
313+
-- element in the source.
314+
--
305315
BBS.lisp.conses.set_car(dest_cons, BBS.lisp.conses.get_car(src_cons));
306-
BBS.lisp.memory.ref(BBS.lisp.conses.get_car(dest_cons));
307316
src_cons := getList(BBS.lisp.conses.get_cdr(src_cons));
308317
if src_cons = NIL_CONS then
309318
exit;

src/bbs-lisp-info.ads

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package BBS.lisp.info is
44
-- the test coverage script. It should not be edited by hand.
55
--
66
name : constant String := "Tiny Lisp";
7-
timestamp : constant String := "Fri Aug 6 11:19:32 MST 2021";
8-
build_date : constant String := "2021-Aug-06 (master)";
7+
timestamp : constant String := "Sun Aug 8 11:50:18 MST 2021";
8+
build_date : constant String := "2021-Aug-08 (master)";
99
version_string : constant String := "V00.03+";
10-
version_date : constant Integer := 20210806; -- yyyymmdd
10+
version_date : constant Integer := 20210808; -- yyyymmdd
1111
version_number : constant Integer := 2;
1212
end;

0 commit comments

Comments
 (0)