git: 9front

Download patch

ref: e076133abd6b14accde867194768467d0e4a3ac7
parent: 354c97d262fecbddad169f636e19b13ae89a962e
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Tue Apr 4 20:34:07 EDT 2017

kernel: avoid waserror() botch in devwalk (from drawterm, thanks aiju)

calculate alloc flag before waserror(), as compilers like
gcc will not notice the value changing later because
setjump() restores the old value due to callee-saves.

change is applies here to make it easier to merge with
drawterm.

thanks to aiju for debugging this; used to cause drawterm
memory leak until compiled with gcc -O0.

--- a/sys/src/9/port/dev.c
+++ b/sys/src/9/port/dev.c
@@ -176,18 +176,17 @@
 	if(nname > 0)
 		isdir(c);
 
-	alloc = 0;
+	alloc = (nc == nil);
 	wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid));
 	if(waserror()){
-		if(alloc && wq->clone!=nil)
+		if(alloc && wq->clone != nil)
 			cclose(wq->clone);
 		free(wq);
 		return nil;
 	}
-	if(nc == nil){
+	if(alloc){
 		nc = devclone(c);
 		nc->type = 0;	/* device doesn't know about this channel yet */
-		alloc = 1;
 	}
 	wq->clone = nc;
 
@@ -252,7 +251,7 @@
 		if(alloc)
 			cclose(wq->clone);
 		wq->clone = nil;
-	}else if(wq->clone){
+	}else if(wq->clone != nil){
 		/* attach cloned channel to same device */
 		wq->clone->type = c->type;
 	}
--