shithub: femtolisp

Download patch

ref: a4bfa20884163ae4a256c48da9eeac47a8fd4402
parent: 1ee781cb64e6fa05bf9b60a0065d4839da546f52
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Wed Nov 13 02:34:48 EST 2024

read: fix a possible crash

If a gigantic expression is loaded, there is a high chance gc
kicked in before ios_eof got called on a non-updated pointer
(unfortunately).

Don't even need a gc handle here.

--- a/iostream.c
+++ b/iostream.c
@@ -117,20 +117,13 @@
 
 BUILTIN("read", read)
 {
-	value_t arg = 0;
 	if(nargs > 1)
 		argcount(nargs, 1);
-	else if(nargs == 0)
-		arg = symbol_value(FL(instrsym));
-	else
-		arg = args[0];
-	ios_t *s = toiostream(arg);
-	fl_gc_handle(&arg);
-	value_t v = fl_read_sexpr(arg);
-	fl_free_gc_handles(1);
-	if(ios_eof(s))
-		return FL(eof);
-	return v;
+	value_t a = nargs == 0 ? symbol_value(FL(instrsym)) : args[0];
+	USED(toiostream(a));
+	value_t v = fl_read_sexpr(a);
+	a = nargs == 0 ? symbol_value(FL(instrsym)) : args[0];
+	return ios_eof(toiostream(a)) ? FL(eof) : v;
 }
 
 BUILTIN("io-getc", io_getc)