shithub: pplay

Download patch

ref: 8d34476e2e8852e577d67f74ada5bf65c5701935
parent: f3c2b5940936fd886f55a3b2afb527cc7cb38a9a
author: qwx <qwx@sciops.net>
date: Thu Dec 8 17:39:36 EST 2022

buffer can grow before totalsz is updated, don't panic

but good idea to make absolutely sure

--- a/cmd.c
+++ b/cmd.c
@@ -43,16 +43,21 @@
 
 #define ASSERT(x) {if(!(x)) printchunks(&norris); assert((x)); }
 static void
-paranoia(void)
+paranoia(int exact)
 {
+	usize n;
 	Chunk *c;
 
 	ASSERT(dot.pos >= dot.from.pos && dot.pos < dot.to.pos);
-	ASSERT(dot.to.pos <= totalsz);
-	for(c=norris.right; c!=&norris; c=c->right){
+	for(c=norris.right, n=0; c!=&norris; c=c->right){
 		ASSERT(c->buf != nil);
 		ASSERT((c->bufsz & 3) == 0 && c->bufsz >= Sampsz);
+		n += c->bufsz;
 	}
+	if(exact){
+		ASSERT(n <= totalsz);
+		ASSERT(dot.to.pos <= totalsz);
+	}
 }
 
 static Chunk *
@@ -644,6 +649,8 @@
 			break;
 		s += n;
 	}
+	if(debug)
+		paranoia(1);
 	switch(r){
 	case '<': x = pipefrom(s); break;
 	case '^': x = pipethrough(s); break;
@@ -659,7 +666,7 @@
 	default: werrstr("unknown command %C", r); x = -1; break;
 	}
 	if(debug)
-		paranoia();
+		paranoia(0);
 	recalcsize();
 	return x;
 }