git: 9front

Download patch

ref: 16f59eb0fe647905bf558e470e5425aab4c012b5
parent: 00712034efcc8c962638d2ab0d2cd98e93d04aa5
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Wed Sep 4 17:27:59 EDT 2013

abaco: fix memory leaks

--- a/sys/src/cmd/abaco/html.c
+++ b/sys/src/cmd/abaco/html.c
@@ -731,7 +731,7 @@
 		l->lastbox->next = b;
 	}
 	l->lastbox = b;
-
+	setmalloctag(b, getcallerpc(&l));
 	return b;
 }
 
@@ -961,8 +961,9 @@
 void
 laypage(Page *p)
 {
-	settables(p);
 	layfree(p->lay);
+	p->lay = nil;
+	settables(p);
 	p->lay = layitems(p->items, Rect(0,0,Dx(p->r),Dy(p->r)), TRUE);
 	p->lay->r.max.y = max(p->lay->r.max.y, Dy(p->r));
 }
@@ -1015,16 +1016,17 @@
 	for(l=lay->lines; l!=nil; l=nextline){
 		for(b=l->boxes; b!=nil; b=nextbox){
 			nextbox = b->next;
-			if(b->i->tag==Iformfieldtag && istextfield(b->i)){
-				aux = &((Iformfield *)b->i)->aux;
-				if(*aux){
-					textclose(*aux);
-					free(*aux);
-				}
-				*aux = nil;
-			}else if(b->i->tag == Itabletag)
-				laytablefree(((Itable *)b->i)->table);
-
+			if(lay->laying==TRUE){
+				if(b->i->tag==Iformfieldtag && istextfield(b->i)){
+					aux = &((Iformfield *)b->i)->aux;
+					if(*aux){
+						textclose(*aux);
+						free(*aux);
+					}
+					*aux = nil;
+				}else if(b->i->tag == Itabletag)
+					laytablefree(((Itable *)b->i)->table);
+			}
 			free(b);
 		}
 		nextline = l->next;
--- a/sys/src/cmd/abaco/util.c
+++ b/sys/src/cmd/abaco/util.c
@@ -443,10 +443,13 @@
 {
 	Biobufhdr *bp;
 	char buf[128];
+	char *s;
 	int i;
 
 	/* we don't care if getenv(2) fails */
-	snprint(buf, sizeof(buf)-1, "%s/lib/abaco.fonts", getenv("home"));
+	s = getenv("home");
+	snprint(buf, sizeof(buf)-1, "%s/lib/abaco.fonts", s);
+	free(s);
 	if((bp=Bopen(buf, OREAD)) == nil)
 		goto Default;
 
@@ -658,32 +661,22 @@
 static void
 execproc(void *v)
 {
-	Channel *sync;
 	Exec *e;
-	int p[2], q[2];
-	char *cmd;
 
 	threadsetname("execproc");
 	e = v;
-	p[0] = e->p[0];
-	p[1] = e->p[1];
-	q[0] = e->q[0];
-	q[1] = e->q[1];
-	cmd = e->cmd;
-	sync = e->sync;
 	rfork(RFFDG);
-	free(e);
-	dup(p[0], 0);
-	close(p[0]);
-	close(p[1]);
-	if(q[0]){
-		dup(q[1], 1);
-		close(q[0]);
-		close(q[1]);
+	dup(e->p[0], 0);
+	close(e->p[0]);
+	close(e->p[1]);
+	if(e->q[0]){
+		dup(e->q[1], 1);
+		close(e->q[0]);
+		close(e->q[1]);
 	}
 	if(!procstderr)
 		close(2);
-	procexecl(sync, "/bin/rc", "rc", "-c", cmd, 0);
+	procexecl(e->sync, "/bin/rc", "rc", "-c", e->cmd, 0);
 	error("can't exec");
 }
 
@@ -690,34 +683,30 @@
 int
 pipeline(int fd, char *cmd, ...)
 {
-	Channel *sync;
 	Exec *e;
-	int p[2], q[2];
 	va_list a;
 
-	if(pipe(p)<0 || pipe(q)<0)
-		error("can't create pipe");
-	close(p[0]);
-	p[0] = fd;
-	sync = chancreate(sizeof(ulong), 0);
-	if(sync == nil)
-		error("can't create channel");
 	e = emalloc(sizeof(Exec));
-	e->p[0] = p[0];
-	e->p[1] = p[1];
-	e->q[0] = q[0];
-	e->q[1] = q[1];
+	if(pipe(e->p)<0 || pipe(e->q)<0)
+		error("can't create pipe");
+	close(e->p[0]);
+	e->p[0] = fd;
 	va_start(a, cmd);
 	e->cmd = vsmprint(cmd, a);
 	va_end(a);
-	e->sync = sync;
+	e->sync = chancreate(sizeof(ulong), 0);
+	if(e->sync == nil)
+		error("can't create channel");
 	proccreate(execproc, e, STACK);
-	recvul(sync);
-	chanfree(sync);
-	close(p[0]);
-	close(p[1]);
-	close(q[1]);
-	return q[0];
+	recvul(e->sync);
+	chanfree(e->sync);
+	free(e->cmd);
+	close(e->p[0]);
+	close(e->p[1]);
+	close(e->q[1]);
+	fd = e->q[0];
+	free(e);
+	return fd;
 }
 
 static
--