shithub: femtolisp

Download patch

ref: ef4fa80a34dc54cd42fe3e0c157dcede5f5ed207
parent: 1fa5ecb395cf9771300fae5da9e38b69e3aa48a9
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Tue Dec 10 21:14:59 EST 2024

map, for-each: fix wrong stack size assumptions

--- a/flisp.c
+++ b/flisp.c
@@ -2062,7 +2062,7 @@
 		lerrorf(FL(ArgError), "too few arguments");
 	intptr_t argSP = args-FL(stack);
 	assert(argSP >= 0 && argSP < FL(nstack));
-	while(FL(sp)+2+nargs > FL(nstack))
+	while(FL(sp)+2+1+nargs >= FL(nstack))
 		grow_stack();
 	uint32_t k = FL(sp);
 	PUSH(FL(Nil));
@@ -2071,7 +2071,7 @@
 		PUSH(FL(stack)[argSP]);
 		for(uint32_t i = 1; i < nargs; i++){
 			if(!iscons(FL(stack)[argSP+i])){
-				POPN(1+i);
+				POPN(2+i);
 				return FL(stack)[k+1];
 			}
 			PUSH(car(FL(stack)[argSP+i]));
@@ -2097,7 +2097,7 @@
 		lerrorf(FL(ArgError), "too few arguments");
 	intptr_t argSP = args-FL(stack);
 	assert(argSP >= 0 && argSP < FL(nstack));
-	if(FL(sp)+2*nargs > FL(nstack))
+	if(FL(sp)+1+2*nargs >= FL(nstack))
 		grow_stack();
 	for(size_t n = 0;; n++){
 		PUSH(FL(stack)[argSP]);