Tweak strlen checks.

This commit is contained in:
Sage Vaillancourt 2022-04-08 20:16:34 -04:00
parent c1580523ee
commit 9d46a618b6
3 changed files with 9 additions and 8 deletions

View File

@ -22,16 +22,12 @@
; Cube a ; Cube a
(def cube (fn (a) (exp a 3))) (def cube (fn (a) (exp a 3)))
(def fib_ (fn (a depth) ( (def fib (fn (a)
(prnl (cat "n: " a))
(if (< a 2) (if (< a 2)
a 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 ; Return the larger of the two
(def max (fn (a b) (if (> a b) a b))) (def max (fn (a b) (if (> a b) a b)))

View File

@ -44,7 +44,6 @@
(def first (fn (list) (at 0 list))) (def first (fn (list) (at 0 list)))
(def up "..")
(def ~ (env "HOME")) (def ~ (env "HOME"))
(def config (cat ~ "/.pebblisp.pbl")) (def config (cat ~ "/.pebblisp.pbl"))

View File

@ -256,9 +256,15 @@ Object substring(Object* params, int length, unused struct Environment* env)
Object string = params[2]; Object string = params[2];
int len = end.number - start.number; 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); return errorWithContext(BAD_PARAMS, string.string);
} }
if (len > stringLen - start.number) {
len = stringLen - start.number;
}
Object substr = withLen(len, TYPE_STRING); Object substr = withLen(len, TYPE_STRING);
snprintf(substr.string, len + 1, "%s", string.string + start.number); snprintf(substr.string, len + 1, "%s", string.string + start.number);
return substr; return substr;