shithub: sl

Download patch

ref: 52e2cfcd26eac1fe0185edf4467456779fe5d323
parent: ab09c60230b5e4ae63e3ff3c0e813dfe0409cddc
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Tue Apr 8 00:45:29 EDT 2025

c-value: zero out if no values were supplied

--- a/src/cvalues.c
+++ b/src/cvalues.c
@@ -750,7 +750,7 @@
 	f(type, v, dest);
 }
 
-// (new type . args)
+// (c-value type . args)
 // this provides (1) a way to allocate values with a shared type for
 // efficiency, (2) a uniform interface for allocating cvalues of any
 // type, including user-defined.
@@ -760,27 +760,29 @@
 		argcount(nargs, 2);
 	sl_v type = args[0];
 	sl_type *ft = get_type(type);
-	sl_v cv;
+	sl_v v;
 	if(ft->eltype != nil){
 		// special case to handle incomplete array types bla[]
 		usize elsz = ft->elsz;
-		usize cnt;
+		usize cnt = 0;
 
 		if(iscons(cdr_(cdr_(type))))
 			cnt = tosize(car_(cdr_(cdr_(type))));
 		else if(nargs == 2)
 			cnt = predict_arrlen(args[1]);
-		else
-			cnt = 0;
-		cv = cvalue(ft, elsz * cnt);
+		v = cvalue(ft, elsz*cnt);
 		if(nargs == 2)
-			cvalue_arr_init(ft, args[1], cvalue_data(cv));
+			cvalue_arr_init(ft, args[1], cvalue_data(v));
+		else
+			memset(cvalue_data(v), 0, elsz*cnt);
 	}else{
-		cv = cvalue(ft, ft->size);
+		v = cvalue(ft, ft->size);
 		if(nargs == 2)
-			cvalue_init(ft, args[1], cvalue_data(cv));
+			cvalue_init(ft, args[1], cvalue_data(v));
+		else
+			memset(cvalue_data(v), 0, ft->size);
 	}
-	return cv;
+	return v;
 }
 
 // NOTE: this only compares lexicographically; it ignores numeric formats
--