shithub: femtolisp

Download patch

ref: a3f28f6f7514b737dc395c93b97708acf45dec7c
parent: b3a21a0ff408e559639f6c31e1a2ab970787567f
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Tue Jun 18 19:35:10 EDT 2024

ios_vprintf: don't use vasprintf

--- a/ios.c
+++ b/ios.c
@@ -966,6 +966,7 @@
 int
 ios_vprintf(ios_t *s, const char *format, va_list args)
 {
+	char buf[256];
 	char *str;
 	int c;
 
@@ -972,37 +973,19 @@
 #if defined(__plan9__)
 	str = vsmprint(format, args);
 	if((c = strlen(str)) >= 0)
-		ios_write(s, str, c);
-	free(str);
 #else
-	va_list al;
-	va_copy(al, args);
-
-	if(s->state == bst_wr && s->bpos < s->maxsize && s->bm != bm_none){
-		int avail = s->maxsize - s->bpos;
-		char *start = s->buf + s->bpos;
-		c = vsnprintf(start, avail, format, args);
-		if(c < 0){
-			va_end(al);
-			return c;
-		}
-		if(c < avail){
-			s->bpos += (size_t)c;
-			_write_update_pos(s);
-			// TODO: only works right if newline is at end
-			if(s->bm == bm_line && llt_memrchr(start, '\n', (size_t)c))
-				ios_flush(s);
-			va_end(al);
-			return c;
-		}
+	if((c = vsnprintf(buf, sizeof(buf), format, args)) < nelem(buf))
+		str = buf;
+	else{
+		str = malloc(c+1);
+		vsnprintf(str, sizeof(c+1), format, args);
 	}
-	c = vasprintf(&str, format, al);
-	if(c >= 0){
-		ios_write(s, str, c);
-		LLT_FREE(str);
-	}
-	va_end(al);
+	if(c > 0)
 #endif
+		ios_write(s, str, c);
+	if(str != buf)
+		free(str);
+
 	return c;
 }