Skip to content

Commit

Permalink
slice(): better checking and clearer error for going past the end
Browse files Browse the repository at this point in the history
  • Loading branch information
aardappel committed Oct 15, 2024
1 parent 367ed08 commit f28d03c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions dev/src/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,11 @@ nfr("slice", "xs,start,size", "A]*II", "A]1",
[](StackPtr &, VM &vm, Value &l, Value &s, Value &e) {
auto size = e.ival();
auto start = s.ival();
if (size < 0) size = l.vval()->len - start;
if (start < 0 || start + size > l.vval()->len)
vm.BuiltinError("slice: values out of range");
if (size < 0) size = std::max((iint)0, l.vval()->len - start);
if (start < 0)
vm.BuiltinError(cat("slice: start cannot be negative: ", start));
if (start + size > l.vval()->len)
vm.BuiltinError(cat("slice: range extends beyond the end: ", start + size, " > ", l.vval()->len));
auto nv = (LVector *)vm.NewVec(0, size, l.vval()->tti);
nv->Append(vm, l.vval(), start, size);
return Value(nv);
Expand Down

0 comments on commit f28d03c

Please sign in to comment.