shithub: riscv

Download patch

ref: 45f2fd3c01768f6483e4d583e4ca06c8e11362f4
parent: 9cddb6ed33051b8d19753e348a2eed3296622397
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Mon Sep 5 14:34:46 EDT 2011

libdraw: fix old subfont leak

--- a/sys/src/libdraw/cloadimage.c
+++ b/sys/src/libdraw/cloadimage.c
@@ -20,7 +20,7 @@
 		maxy = atoi((char*)data+0*12);
 		nb = atoi((char*)data+1*12);
 		if(maxy<=miny || r.max.y<maxy){
-			werrstr("creadimage: bad maxy %d", maxy);
+			werrstr("cloadimage: bad maxy %d", maxy);
 			return -1;
 		}
 		data += 2*12;
@@ -27,7 +27,7 @@
 		ndata -= 2*12;
 		m += 2*12;
 		if(nb<=0 || ncblock<nb || nb>ndata){
-			werrstr("creadimage: bad count %d", nb);
+			werrstr("cloadimage: bad count %d", nb);
 			return -1;
 		}
 		a = bufimage(i->display, 21+nb);
--- a/sys/src/libdraw/creadimage.c
+++ b/sys/src/libdraw/creadimage.c
@@ -58,7 +58,6 @@
 		if(dolock)
 			lockdisplay(d);
 		i = allocimage(d, r, chan, 0, 0);
-		setmalloctag(i, getcallerpc(&d));
 		if(dolock)
 			unlockdisplay(d);
 		if(i == nil)
@@ -68,6 +67,7 @@
 		if(i == nil)
 			return nil;
 	}
+	setmalloctag(i, getcallerpc(&d));
 	ncblock = _compblocksize(r, chantodepth(chan));
 	buf = malloc(ncblock);
 	if(buf == nil)
--- a/sys/src/libdraw/freesubfont.c
+++ b/sys/src/libdraw/freesubfont.c
@@ -5,10 +5,7 @@
 void
 freesubfont(Subfont *f)
 {
-	if(f == 0)
-		return;
-	f->ref--;
-	if(f->ref > 0)
+	if(f == nil || --f->ref)
 		return;
 	uninstallsubfont(f);
 	free(f->name);
--- a/sys/src/libdraw/getsubfont.c
+++ b/sys/src/libdraw/getsubfont.c
@@ -13,7 +13,6 @@
 	Subfont *f;
 
 	fd = open(name, OREAD);
-		
 	if(fd < 0){
 		fprint(2, "getsubfont: can't open %s: %r\n", name);
 		return 0;
@@ -29,9 +28,8 @@
 	f = readsubfont(d, name, fd, d && d->locking==0);
 	if(d && d->locking == 0)
 		lockdisplay(d);
-	if(f == 0)
-		fprint(2, "getsubfont: can't read %s: %r\n", name);
 	close(fd);
-	setmalloctag(f, getcallerpc(&d));
+	if(f == 0)
+		fprint(2, "_getsubfont: can't read %s: %r\n", name);
 	return f;
 }
--- a/sys/src/libdraw/readimage.c
+++ b/sys/src/libdraw/readimage.c
@@ -19,8 +19,12 @@
 
 	if(readn(fd, hdr, 11) != 11)
 		return nil;
-	if(memcmp(hdr, "compressed\n", 11) == 0)
-		return creadimage(d, fd, dolock);
+	if(memcmp(hdr, "compressed\n", 11) == 0){
+		if(i = creadimage(d, fd, dolock))
+			goto Done;
+		return nil;
+	}
+		
 	if(readn(fd, hdr+11, 5*12-11) != 5*12-11)
 		return nil;
 	if(d)
@@ -123,5 +127,7 @@
 		miny += dy;
 	}
 	free(tmp);
+   Done:
+	setmalloctag(i, getcallerpc(&d));
 	return i;
 }
--- a/sys/src/libdraw/string.c
+++ b/sys/src/libdraw/string.c
@@ -134,11 +134,8 @@
 				else
 					break;
 			}
-			/* 
-			 * must not free sf until cachechars has found it in the cache
-			 * and picked up its own reference.
-			 */
 		}
 	}
+	freesubfont(sf);
 	return pt;
 }
--- a/sys/src/libdraw/stringwidth.c
+++ b/sys/src/libdraw/stringwidth.c
@@ -12,6 +12,7 @@
 	Rune rune, **rptr;
 	char *subfontname, **sptr;
 	Font *def;
+	Subfont *sf;
 
 	if(s == nil){
 		s = "";
@@ -23,6 +24,7 @@
 		rptr = nil;
 	}else
 		rptr = &r;
+	sf = nil;
 	twid = 0;
 	while(len>0 && (*s || *r)){
 		max = Max;
@@ -43,7 +45,8 @@
 				return twid;
 			}
 			if(subfontname){
-				if(_getsubfont(f->display, subfontname) == 0){
+				freesubfont(sf);
+				if((sf=_getsubfont(f->display, subfontname)) == 0){
 					def = f->display->defaultfont;
 					if(def && f!=def)
 						f = def;
@@ -56,6 +59,7 @@
 		twid += wid;
 		len -= l;
 	}
+	freesubfont(sf);
 	return twid;
 }
 
--- a/sys/src/libdraw/subfont.c
+++ b/sys/src/libdraw/subfont.c
@@ -20,8 +20,7 @@
 	f->ref = 1;
 	if(name){
 		f->name = strdup(name);
-		if(lookupsubfont(i->display, name) == 0)
-			installsubfont(name, f);
+		installsubfont(name, f);
 	}else
 		f->name = 0;
 	return f;
--- a/sys/src/libdraw/subfontcache.c
+++ b/sys/src/libdraw/subfontcache.c
@@ -7,7 +7,7 @@
  */
 
 static char	*lastname;
-Subfont	*lastsubfont;
+static Subfont	*lastsubfont;
 
 Subfont*
 lookupsubfont(Display *d, char *name)