shithub: asif

Download patch

ref: 322c0644767d483159be5a07002d36f07ef0172d
parent: 02c88f6e0f1e29884c9d084c790e09e658d47890
author: qwx <qwx@sciops.net>
date: Sun May 29 20:17:16 EDT 2022

varray: fix memory corruption, sanity check and add a nuker

--- a/varray.c
+++ b/varray.c
@@ -16,6 +16,15 @@
 }
 
 void
+vnuke(VArray *v)
+{
+	if(v == nil)
+		return;
+	memset(v->p, 0, v->bufsize);
+	v->n = 0;
+}
+
+void
 vinsert(VArray *v, char *u)
 {
 	int off;
@@ -22,7 +31,7 @@
 
 	assert(v != nil && u != nil);
 	off = v->n * v->elsize;
-	if(v->n++ >= v->bufsize){
+	if(v->n++ >= v->vsize){
 		v->p = erealloc(v->p, v->bufsize * 2, v->bufsize);
 		v->bufsize *= 2;
 		v->vsize *= 2;
@@ -38,6 +47,8 @@
 	v = emalloc(sizeof *v);
 	v->n = 0;
 	v->elsize = elsize;
+	if(n <= 0)
+		n = VAdefsize;
 	v->vsize = MIN(n, VAdefsize);
 	v->bufsize = v->vsize * elsize;
 	v->p = emalloc(v->bufsize);