shithub: riscv

Download patch

ref: 2259f3fb9aebb2973ee2d892bec944f177a41c64
parent: fc1ff7705b339d480f15fa934c7df215158c1901
author: cinap_lenrek <[email protected]>
date: Mon Mar 2 06:01:12 EST 2015

libdraw: font->display->defaultsubfont vs. display->defaultsubfont, dead code, malloc erros

it is possible to have fonts belong to different or no display, so the
check for defaultsubfont has to be against font->display, not the global
display variable.

remove unused freeup() routine.

handle strdup() error in allocsubfont() and realloc() error in buildfont().

--- a/sys/src/libdraw/buildfont.c
+++ b/sys/src/libdraw/buildfont.c
@@ -14,7 +14,7 @@
 buildfont(Display *d, char *buf, char *name)
 {
 	Font *fnt;
-	Cachefont *c;
+	Cachefont *c, **sub;
 	char *s, *t;
 	ulong min, max;
 	int offset;
@@ -22,8 +22,8 @@
 
 	s = buf;
 	fnt = malloc(sizeof(Font));
-	if(fnt == 0)
-		return 0;
+	if(fnt == nil)
+		return nil;
 	memset(fnt, 0, sizeof(Font));
 	fnt->display = d;
 	fnt->name = strdup(name);
@@ -31,7 +31,7 @@
 	fnt->nsubf = NFSUBF;
 	fnt->cache = malloc(fnt->ncache * sizeof(fnt->cache[0]));
 	fnt->subf = malloc(fnt->nsubf * sizeof(fnt->subf[0]));
-	if(fnt->name==0 || fnt->cache==0 || fnt->subf==0){
+	if(fnt->name==nil || fnt->cache==nil || fnt->subf==nil){
     Err2:
 		free(fnt->name);
 		free(fnt->cache);
@@ -38,7 +38,7 @@
 		free(fnt->subf);
 		free(fnt->sub);
 		free(fnt);
-		return 0;
+		return nil;
 	}
 	fnt->height = strtol(s, &s, 0);
 	s = skip(s);
@@ -50,7 +50,7 @@
 	}
 	fnt->width = 0;
 	fnt->nsub = 0;
-	fnt->sub = 0;
+	fnt->sub = nil;
 
 	memset(fnt->subf, 0, fnt->nsubf * sizeof(fnt->subf[0]));
 	memset(fnt->cache, 0, fnt->ncache*sizeof(fnt->cache[0]));
@@ -82,16 +82,13 @@
 			s = skip(t);
 		else
 			offset = 0;
-		fnt->sub = realloc(fnt->sub, (fnt->nsub+1)*sizeof(Cachefont*));
-		if(fnt->sub == 0){
-			/* realloc manual says fnt->sub may have been destroyed */
-			fnt->nsub = 0;
+		sub = realloc(fnt->sub, (fnt->nsub+1)*sizeof(Cachefont*));
+		if(sub == nil)
 			goto Err3;
-		}
+		fnt->sub = sub;
 		c = malloc(sizeof(Cachefont));
-		if(c == 0)
+		if(c == nil)
 			goto Err3;
-		fnt->sub[fnt->nsub] = c;
 		c->min = min;
 		c->max = max;
 		c->offset = offset;
@@ -99,14 +96,14 @@
 		while(*s && *s!=' ' && *s!='\n' && *s!='\t')
 			s++;
 		*s++ = 0;
-		c->subfontname = 0;
+		c->subfontname = nil;
 		c->name = strdup(t);
-		if(c->name == 0){
+		if(c->name == nil){
 			free(c);
 			goto Err3;
 		}
+		sub[fnt->nsub++] = c;
 		s = skip(s);
-		fnt->nsub++;
 	}while(*s);
 	return fnt;
 }
@@ -118,7 +115,7 @@
 	Cachefont *c;
 	Subfont *s;
 
-	if(f == 0)
+	if(f == nil)
 		return;
 
 	for(i=0; i<f->nsub; i++){
@@ -129,9 +126,10 @@
 	}
 	for(i=0; i<f->nsubf; i++){
 		s = f->subf[i].f;
-		if(s)
-			if(display == nil || s!=display->defaultsubfont)
+		if(s != nil){
+			if(f->display == nil || s != f->display->defaultsubfont)
 				freesubfont(s);
+		}
 	}
 	freeimage(f->cacheimage);
 	free(f->name);
--- a/sys/src/libdraw/font.c
+++ b/sys/src/libdraw/font.c
@@ -3,7 +3,6 @@
 #include <draw.h>
 
 static int	fontresize(Font*, int, int, int);
-static int	freeup(Font*);
 
 #define	PJW	0	/* use NUL==pjw for invisible characters */
 
@@ -129,7 +128,7 @@
 			if(s->age){
 				if(s->age<SUBFAGE && s->cf->name != nil){
 					/* clean up */
-					if(display == nil || s->f != display->defaultsubfont)
+					if(f->display == nil || s->f != f->display->defaultsubfont)
 						freesubfont(s->f);
 					s->cf = nil;
 					s->f = nil;
@@ -314,32 +313,6 @@
 	b[35] = fi->left;
 	b[36] = fi->width;
 	return 1;
-}
-
-/* release all subfonts, return number freed */
-static
-int
-freeup(Font *f)
-{
-	Cachesubf *s, *es;
-	int nf;
-
-	if(f->sub[0]->name == nil)	/* font from mkfont; don't free */
-		return 0;
-	s = f->subf;
-	es = s+f->nsubf;
-	nf = 0;
-	while(s < es){
-		if(s->age){
-			freesubfont(s->f);
-			s->cf = nil;
-			s->f = nil;
-			s->age = 0;
-			nf++;
-		}
-		s++;
-	}
-	return nf;
 }
 
 /* returns whether resize succeeded && f->cache is unchanged */
--- a/sys/src/libdraw/mkfont.c
+++ b/sys/src/libdraw/mkfont.c
@@ -12,8 +12,8 @@
 	Cachefont *c;
 
 	font = malloc(sizeof(Font));
-	if(font == 0)
-		return 0;
+	if(font == nil)
+		return nil;
 	memset(font, 0, sizeof(Font));
 	font->display = subfont->bits->display;
 	font->name = strdup("<synthetic>");
@@ -21,7 +21,7 @@
 	font->nsubf = NFSUBF;
 	font->cache = malloc(font->ncache * sizeof(font->cache[0]));
 	font->subf = malloc(font->nsubf * sizeof(font->subf[0]));
-	if(font->name==0 || font->cache==0 || font->subf==0){
+	if(font->name==nil || font->cache==nil || font->subf==nil){
     Err:
 		free(font->name);
 		free(font->cache);
@@ -36,10 +36,10 @@
 	font->ascent = subfont->ascent;
 	font->age = 1;
 	font->sub = malloc(sizeof(Cachefont*));
-	if(font->sub == 0)
+	if(font->sub == nil)
 		goto Err;
 	c = malloc(sizeof(Cachefont));
-	if(c == 0)
+	if(c == nil)
 		goto Err;
 	font->nsub = 1;
 	font->sub[0] = c;
@@ -46,8 +46,8 @@
 	c->min = min;
 	c->max = min+subfont->n-1;
 	c->offset = 0;
-	c->name = 0;	/* noticed by freeup() and agefont() */
-	c->subfontname = 0;
+	c->name = nil;	/* noticed by agefont() */
+	c->subfontname = nil;
 	font->subf[0].age = 0;
 	font->subf[0].cf = c;
 	font->subf[0].f = subfont;
--- a/sys/src/libdraw/subfont.c
+++ b/sys/src/libdraw/subfont.c
@@ -10,8 +10,8 @@
 	assert(height != 0 /* allocsubfont */);
 
 	f = malloc(sizeof(Subfont));
-	if(f == 0)
-		return 0;
+	if(f == nil)
+		return nil;
 	f->n = n;
 	f->height = height;
 	f->ascent = ascent;
@@ -20,8 +20,12 @@
 	f->ref = 1;
 	if(name){
 		f->name = strdup(name);
+		if(f->name == nil){
+			free(f);
+			return nil;
+		}
 		installsubfont(name, f);
 	}else
-		f->name = 0;
+		f->name = nil;
 	return f;
 }