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().
This commit is contained in:
cinap_lenrek 2015-03-02 11:01:12 +01:00
parent fc1ff7705b
commit 2259f3fb9a
4 changed files with 32 additions and 57 deletions

View file

@ -14,7 +14,7 @@ Font*
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 @@ buildfont(Display *d, char *buf, char *name)
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,14 +31,14 @@ buildfont(Display *d, char *buf, char *name)
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);
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 @@ buildfont(Display *d, char *buf, char *name)
}
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 @@ buildfont(Display *d, char *buf, char *name)
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 @@ buildfont(Display *d, char *buf, char *name)
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 @@ freefont(Font *f)
Cachefont *c;
Subfont *s;
if(f == 0)
if(f == nil)
return;
for(i=0; i<f->nsub; i++){
@ -129,9 +126,10 @@ freefont(Font *f)
}
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);

View file

@ -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 @@ agefont(Font *f)
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;
@ -316,32 +315,6 @@ loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
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 */
static int
fontresize(Font *f, int wid, int ncache, int depth)

View file

@ -12,8 +12,8 @@ mkfont(Subfont *subfont, Rune min)
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 @@ mkfont(Subfont *subfont, Rune min)
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,18 +36,18 @@ mkfont(Subfont *subfont, Rune min)
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;
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;

View file

@ -10,8 +10,8 @@ allocsubfont(char *name, int n, int height, int ascent, Fontchar *info, Image *i
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 @@ allocsubfont(char *name, int n, int height, int ascent, Fontchar *info, Image *i
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;
}