shithub: femtolisp

Download patch

ref: c4d72d5376716b78fbe292be1d21697823b92566
parent: 96c2da5fef670527ba6a7655917803bc22e83e79
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Oct 30 15:43:20 EDT 2024

arrays, vectors: catch index < 0 and produce an error

--- a/cvalues.c
+++ b/cvalues.c
@@ -840,7 +840,7 @@
 	*data = cv_data(cv);
 	numel = cv_len(cv)/cv_class(cv)->elsz;
 	*index = toulong(ind);
-	if(*index >= numel)
+	if(*index < 0 || *index >= numel)
 		bounds_error(arr, ind);
 }
 
--- a/flisp.c
+++ b/flisp.c
@@ -1223,7 +1223,7 @@
 			if(isvector(v)){
 				e = Stack[SP-1];
 				i = isfixnum(e) ? numval(e) : (uint32_t)toulong(e);
-				if((unsigned)i >= vector_size(v))
+				if(i < 0 || i >= vector_size(v))
 					bounds_error(v, e);
 				v = vector_elt(v, i);
 			}else if(isarray(v)){
@@ -1587,7 +1587,7 @@
 			e = Stack[SP-3];
 			if(isvector(e)){
 				i = tofixnum(Stack[SP-2]);
-				if((unsigned)i >= vector_size(e))
+				if(i < 0 || i >= vector_size(e))
 					bounds_error(v, Stack[SP-1]);
 				vector_elt(e, i) = (v = Stack[SP-1]);
 			}else if(isarray(e)){