shithub: riscv

Download patch

ref: d2a1d1fb37eb8df28a3a07263519580c1c8450bf
parent: e1260aa4d6ff21ef374ca05bf13aecb16fca1f94
author: aiju <[email protected]>
date: Mon Jul 25 06:10:11 EDT 2011

devshr: fixed memory leaks / race condition

--- a/sys/src/9/port/devshr.c
+++ b/sys/src/9/port/devshr.c
@@ -29,8 +29,12 @@
 shrdecref(Shr *sp)
 {
 	Mount *m, *mm;
+	int n;
 
-	if(decref(sp) != 0)
+	qlock(&shrlk);
+	n = decref(sp);
+	qunlock(&shrlk);
+	if(n != 0)
 		return;
 	
 	for(m = sp->umh.mount; m != nil; m = mm) {
@@ -38,6 +42,8 @@
 		mm = m->next;
 		free(m);
 	}
+	if(sp->desc != nil)
+		free(sp->desc);
 	free(sp->owner);
 	free(sp->name);
 	free(sp);
@@ -491,6 +497,7 @@
 	}
 	ret = readstr(off, va, n, sp->desc);
 	qunlock(&sp->desclock);
+	shrdecref(sp);
 	return ret;
 }
 
@@ -511,19 +518,14 @@
 	
 	qlock(&shrlk);
 	sp = shrlookup(nil, c->qid.path);
-	if(sp == nil) {
-		qunlock(&shrlk);
-		error(Enonexist);
-	}
-	incref(sp);
+	if(sp != nil)
+		incref(sp);
 	qunlock(&shrlk);
-	if(waserror()){
-		shrdecref(sp);
-		nexterror();
-	}
-
+	if(sp == nil)
+		error(Enonexist);
 	buf = smalloc(n+1);
 	if(waserror()){
+		shrdecref(sp);
 		free(buf);
 		nexterror();
 	}
@@ -542,7 +544,6 @@
 		shrdecref(sp);
 		free(buf);
 		poperror();
-		poperror();
 		return n;
 	}
 	
@@ -615,7 +616,6 @@
 	qunlock(&sp->desclock);
 	shrdecref(sp);
 	free(buf);
-	poperror();
 	poperror();
 	return n;
 }