From 6ca5a76286a74c8856a19cee1c0f548b51842953 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 4 May 2017 11:34:24 -0400 Subject: [PATCH] Fix a divide by 0 error when table is too small. --- core/ds.c | 4 +++- libs/compile.gst | 3 --- libs/pp.gst | 9 +++------ 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/core/ds.c b/core/ds.c index 61a391f8..e3b13afa 100644 --- a/core/ds.c +++ b/core/ds.c @@ -176,7 +176,9 @@ void *gst_userdata(Gst *vm, uint32_t size, const GstUserType *utype) { /* Create a new table */ GstTable *gst_table(Gst *vm, uint32_t capacity) { GstTable *t = gst_alloc(vm, sizeof(GstTable)); - GstValue *data = gst_zalloc(vm, capacity * sizeof(GstValue)); + GstValue *data; + if (capacity < 2) capacity = 2; + data = gst_zalloc(vm, capacity * sizeof(GstValue)); t->data = data; t->capacity = capacity; t->count = 0; diff --git a/libs/compile.gst b/libs/compile.gst index 12d5b721..e743757d 100644 --- a/libs/compile.gst +++ b/libs/compile.gst @@ -25,6 +25,3 @@ 'literalsArray [] 'slotMap [] })) - -# Push a scope onto the compiler - diff --git a/libs/pp.gst b/libs/pp.gst index f379190d..0fbed1d8 100644 --- a/libs/pp.gst +++ b/libs/pp.gst @@ -6,7 +6,7 @@ # Pretty print an array or tuple (: print-seq (fn [start end a seen] (: seen (if seen seen {})) - (if (get seen s) (get seen s) + (if (get seen a) (get seen a) (do (: parts []) (: len (length a)) @@ -18,7 +18,7 @@ (if (> len 0) (pop! parts)) (push! parts end) (: ret (apply string start parts)) - (set! seen s ret) + (set! seen a ret) ret)))) # Pretty print an object or struct @@ -53,7 +53,4 @@ (: h (get handlers (type x))) ((if h h tostring) x seen))) -(print (pp [1 {4 5 6 7} 2 3])) - -# Module export pattern - last expression in file is value of module -pp +# (print (pp [1 {4 5 6 7} 2 3]))