From 9d46a618b6e6a4662044f117a92327cdc29188c5 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Fri, 8 Apr 2022 20:16:34 -0400 Subject: [PATCH] Tweak strlen checks. --- src/examples/lib.pbl | 8 ++------ src/examples/pebblisp.pbl | 1 - src/plfunc.c | 8 +++++++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/examples/lib.pbl b/src/examples/lib.pbl index 4f5e7ab..070073a 100644 --- a/src/examples/lib.pbl +++ b/src/examples/lib.pbl @@ -22,16 +22,12 @@ ; Cube a (def cube (fn (a) (exp a 3))) -(def fib_ (fn (a depth) ( - (prnl (cat "n: " a)) +(def fib (fn (a) (if (< a 2) a - (+ (fib_ (- a 1) (+ 1 depth)) (fib_ (- a 2) (+ 1 depth))) - ) + (+ (fib (- a 1)) (fib (- a 2))) ))) -(def fib (fn (a) ((fib a 0)))) - ; Return the larger of the two (def max (fn (a b) (if (> a b) a b))) diff --git a/src/examples/pebblisp.pbl b/src/examples/pebblisp.pbl index 0ad409a..197ed30 100644 --- a/src/examples/pebblisp.pbl +++ b/src/examples/pebblisp.pbl @@ -44,7 +44,6 @@ (def first (fn (list) (at 0 list))) -(def up "..") (def ~ (env "HOME")) (def config (cat ~ "/.pebblisp.pbl")) diff --git a/src/plfunc.c b/src/plfunc.c index e52d34b..a145e10 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -256,9 +256,15 @@ Object substring(Object* params, int length, unused struct Environment* env) Object string = params[2]; int len = end.number - start.number; - if (len < 0 || start.number >= strlen(string.string)) { + size_t stringLen = strlen(string.string); + if (len < 0 || start.number > stringLen) { return errorWithContext(BAD_PARAMS, string.string); } + + if (len > stringLen - start.number) { + len = stringLen - start.number; + } + Object substr = withLen(len, TYPE_STRING); snprintf(substr.string, len + 1, "%s", string.string + start.number); return substr;