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().
front
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) buildfont(Display *d, char *buf, char *name)
{ {
Font *fnt; Font *fnt;
Cachefont *c; Cachefont *c, **sub;
char *s, *t; char *s, *t;
ulong min, max; ulong min, max;
int offset; int offset;
@ -22,8 +22,8 @@ buildfont(Display *d, char *buf, char *name)
s = buf; s = buf;
fnt = malloc(sizeof(Font)); fnt = malloc(sizeof(Font));
if(fnt == 0) if(fnt == nil)
return 0; return nil;
memset(fnt, 0, sizeof(Font)); memset(fnt, 0, sizeof(Font));
fnt->display = d; fnt->display = d;
fnt->name = strdup(name); fnt->name = strdup(name);
@ -31,14 +31,14 @@ buildfont(Display *d, char *buf, char *name)
fnt->nsubf = NFSUBF; fnt->nsubf = NFSUBF;
fnt->cache = malloc(fnt->ncache * sizeof(fnt->cache[0])); fnt->cache = malloc(fnt->ncache * sizeof(fnt->cache[0]));
fnt->subf = malloc(fnt->nsubf * sizeof(fnt->subf[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: Err2:
free(fnt->name); free(fnt->name);
free(fnt->cache); free(fnt->cache);
free(fnt->subf); free(fnt->subf);
free(fnt->sub); free(fnt->sub);
free(fnt); free(fnt);
return 0; return nil;
} }
fnt->height = strtol(s, &s, 0); fnt->height = strtol(s, &s, 0);
s = skip(s); s = skip(s);
@ -50,7 +50,7 @@ buildfont(Display *d, char *buf, char *name)
} }
fnt->width = 0; fnt->width = 0;
fnt->nsub = 0; fnt->nsub = 0;
fnt->sub = 0; fnt->sub = nil;
memset(fnt->subf, 0, fnt->nsubf * sizeof(fnt->subf[0])); memset(fnt->subf, 0, fnt->nsubf * sizeof(fnt->subf[0]));
memset(fnt->cache, 0, fnt->ncache*sizeof(fnt->cache[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); s = skip(t);
else else
offset = 0; offset = 0;
fnt->sub = realloc(fnt->sub, (fnt->nsub+1)*sizeof(Cachefont*)); sub = realloc(fnt->sub, (fnt->nsub+1)*sizeof(Cachefont*));
if(fnt->sub == 0){ if(sub == nil)
/* realloc manual says fnt->sub may have been destroyed */
fnt->nsub = 0;
goto Err3; goto Err3;
} fnt->sub = sub;
c = malloc(sizeof(Cachefont)); c = malloc(sizeof(Cachefont));
if(c == 0) if(c == nil)
goto Err3; goto Err3;
fnt->sub[fnt->nsub] = c;
c->min = min; c->min = min;
c->max = max; c->max = max;
c->offset = offset; c->offset = offset;
@ -99,14 +96,14 @@ buildfont(Display *d, char *buf, char *name)
while(*s && *s!=' ' && *s!='\n' && *s!='\t') while(*s && *s!=' ' && *s!='\n' && *s!='\t')
s++; s++;
*s++ = 0; *s++ = 0;
c->subfontname = 0; c->subfontname = nil;
c->name = strdup(t); c->name = strdup(t);
if(c->name == 0){ if(c->name == nil){
free(c); free(c);
goto Err3; goto Err3;
} }
sub[fnt->nsub++] = c;
s = skip(s); s = skip(s);
fnt->nsub++;
}while(*s); }while(*s);
return fnt; return fnt;
} }
@ -118,7 +115,7 @@ freefont(Font *f)
Cachefont *c; Cachefont *c;
Subfont *s; Subfont *s;
if(f == 0) if(f == nil)
return; return;
for(i=0; i<f->nsub; i++){ for(i=0; i<f->nsub; i++){
@ -129,9 +126,10 @@ freefont(Font *f)
} }
for(i=0; i<f->nsubf; i++){ for(i=0; i<f->nsubf; i++){
s = f->subf[i].f; s = f->subf[i].f;
if(s) if(s != nil){
if(display == nil || s!=display->defaultsubfont) if(f->display == nil || s != f->display->defaultsubfont)
freesubfont(s); freesubfont(s);
}
} }
freeimage(f->cacheimage); freeimage(f->cacheimage);
free(f->name); free(f->name);

View File

@ -3,7 +3,6 @@
#include <draw.h> #include <draw.h>
static int fontresize(Font*, int, int, int); static int fontresize(Font*, int, int, int);
static int freeup(Font*);
#define PJW 0 /* use NUL==pjw for invisible characters */ #define PJW 0 /* use NUL==pjw for invisible characters */
@ -129,7 +128,7 @@ agefont(Font *f)
if(s->age){ if(s->age){
if(s->age<SUBFAGE && s->cf->name != nil){ if(s->age<SUBFAGE && s->cf->name != nil){
/* clean up */ /* clean up */
if(display == nil || s->f != display->defaultsubfont) if(f->display == nil || s->f != f->display->defaultsubfont)
freesubfont(s->f); freesubfont(s->f);
s->cf = nil; s->cf = nil;
s->f = nil; s->f = nil;
@ -316,32 +315,6 @@ loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
return 1; 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 */ /* returns whether resize succeeded && f->cache is unchanged */
static int static int
fontresize(Font *f, int wid, int ncache, int depth) fontresize(Font *f, int wid, int ncache, int depth)

View File

@ -12,8 +12,8 @@ mkfont(Subfont *subfont, Rune min)
Cachefont *c; Cachefont *c;
font = malloc(sizeof(Font)); font = malloc(sizeof(Font));
if(font == 0) if(font == nil)
return 0; return nil;
memset(font, 0, sizeof(Font)); memset(font, 0, sizeof(Font));
font->display = subfont->bits->display; font->display = subfont->bits->display;
font->name = strdup("<synthetic>"); font->name = strdup("<synthetic>");
@ -21,7 +21,7 @@ mkfont(Subfont *subfont, Rune min)
font->nsubf = NFSUBF; font->nsubf = NFSUBF;
font->cache = malloc(font->ncache * sizeof(font->cache[0])); font->cache = malloc(font->ncache * sizeof(font->cache[0]));
font->subf = malloc(font->nsubf * sizeof(font->subf[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: Err:
free(font->name); free(font->name);
free(font->cache); free(font->cache);
@ -36,18 +36,18 @@ mkfont(Subfont *subfont, Rune min)
font->ascent = subfont->ascent; font->ascent = subfont->ascent;
font->age = 1; font->age = 1;
font->sub = malloc(sizeof(Cachefont*)); font->sub = malloc(sizeof(Cachefont*));
if(font->sub == 0) if(font->sub == nil)
goto Err; goto Err;
c = malloc(sizeof(Cachefont)); c = malloc(sizeof(Cachefont));
if(c == 0) if(c == nil)
goto Err; goto Err;
font->nsub = 1; font->nsub = 1;
font->sub[0] = c; font->sub[0] = c;
c->min = min; c->min = min;
c->max = min+subfont->n-1; c->max = min+subfont->n-1;
c->offset = 0; c->offset = 0;
c->name = 0; /* noticed by freeup() and agefont() */ c->name = nil; /* noticed by agefont() */
c->subfontname = 0; c->subfontname = nil;
font->subf[0].age = 0; font->subf[0].age = 0;
font->subf[0].cf = c; font->subf[0].cf = c;
font->subf[0].f = subfont; 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 */); assert(height != 0 /* allocsubfont */);
f = malloc(sizeof(Subfont)); f = malloc(sizeof(Subfont));
if(f == 0) if(f == nil)
return 0; return nil;
f->n = n; f->n = n;
f->height = height; f->height = height;
f->ascent = ascent; f->ascent = ascent;
@ -20,8 +20,12 @@ allocsubfont(char *name, int n, int height, int ascent, Fontchar *info, Image *i
f->ref = 1; f->ref = 1;
if(name){ if(name){
f->name = strdup(name); f->name = strdup(name);
if(f->name == nil){
free(f);
return nil;
}
installsubfont(name, f); installsubfont(name, f);
}else }else
f->name = 0; f->name = nil;
return f; return f;
} }