libdraw: fix old subfont leak

This commit is contained in:
cinap_lenrek 2011-09-05 18:34:46 +02:00
parent 9cddb6ed33
commit 45f2fd3c01
9 changed files with 22 additions and 21 deletions

View file

@ -20,14 +20,14 @@ cloadimage(Image *i, Rectangle r, uchar *data, int ndata)
maxy = atoi((char*)data+0*12); maxy = atoi((char*)data+0*12);
nb = atoi((char*)data+1*12); nb = atoi((char*)data+1*12);
if(maxy<=miny || r.max.y<maxy){ if(maxy<=miny || r.max.y<maxy){
werrstr("creadimage: bad maxy %d", maxy); werrstr("cloadimage: bad maxy %d", maxy);
return -1; return -1;
} }
data += 2*12; data += 2*12;
ndata -= 2*12; ndata -= 2*12;
m += 2*12; m += 2*12;
if(nb<=0 || ncblock<nb || nb>ndata){ if(nb<=0 || ncblock<nb || nb>ndata){
werrstr("creadimage: bad count %d", nb); werrstr("cloadimage: bad count %d", nb);
return -1; return -1;
} }
a = bufimage(i->display, 21+nb); a = bufimage(i->display, 21+nb);

View file

@ -58,7 +58,6 @@ creadimage(Display *d, int fd, int dolock)
if(dolock) if(dolock)
lockdisplay(d); lockdisplay(d);
i = allocimage(d, r, chan, 0, 0); i = allocimage(d, r, chan, 0, 0);
setmalloctag(i, getcallerpc(&d));
if(dolock) if(dolock)
unlockdisplay(d); unlockdisplay(d);
if(i == nil) if(i == nil)
@ -68,6 +67,7 @@ creadimage(Display *d, int fd, int dolock)
if(i == nil) if(i == nil)
return nil; return nil;
} }
setmalloctag(i, getcallerpc(&d));
ncblock = _compblocksize(r, chantodepth(chan)); ncblock = _compblocksize(r, chantodepth(chan));
buf = malloc(ncblock); buf = malloc(ncblock);
if(buf == nil) if(buf == nil)

View file

@ -5,10 +5,7 @@
void void
freesubfont(Subfont *f) freesubfont(Subfont *f)
{ {
if(f == 0) if(f == nil || --f->ref)
return;
f->ref--;
if(f->ref > 0)
return; return;
uninstallsubfont(f); uninstallsubfont(f);
free(f->name); free(f->name);

View file

@ -13,7 +13,6 @@ _getsubfont(Display *d, char *name)
Subfont *f; Subfont *f;
fd = open(name, OREAD); fd = open(name, OREAD);
if(fd < 0){ if(fd < 0){
fprint(2, "getsubfont: can't open %s: %r\n", name); fprint(2, "getsubfont: can't open %s: %r\n", name);
return 0; return 0;
@ -29,9 +28,8 @@ _getsubfont(Display *d, char *name)
f = readsubfont(d, name, fd, d && d->locking==0); f = readsubfont(d, name, fd, d && d->locking==0);
if(d && d->locking == 0) if(d && d->locking == 0)
lockdisplay(d); lockdisplay(d);
if(f == 0)
fprint(2, "getsubfont: can't read %s: %r\n", name);
close(fd); close(fd);
setmalloctag(f, getcallerpc(&d)); if(f == 0)
fprint(2, "_getsubfont: can't read %s: %r\n", name);
return f; return f;
} }

View file

@ -19,8 +19,12 @@ readimage(Display *d, int fd, int dolock)
if(readn(fd, hdr, 11) != 11) if(readn(fd, hdr, 11) != 11)
return nil; return nil;
if(memcmp(hdr, "compressed\n", 11) == 0) if(memcmp(hdr, "compressed\n", 11) == 0){
return creadimage(d, fd, dolock); if(i = creadimage(d, fd, dolock))
goto Done;
return nil;
}
if(readn(fd, hdr+11, 5*12-11) != 5*12-11) if(readn(fd, hdr+11, 5*12-11) != 5*12-11)
return nil; return nil;
if(d) if(d)
@ -123,5 +127,7 @@ readimage(Display *d, int fd, int dolock)
miny += dy; miny += dy;
} }
free(tmp); free(tmp);
Done:
setmalloctag(i, getcallerpc(&d));
return i; return i;
} }

View file

@ -134,11 +134,8 @@ _string(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s, Rune *r, i
else else
break; break;
} }
/*
* must not free sf until cachechars has found it in the cache
* and picked up its own reference.
*/
} }
} }
freesubfont(sf);
return pt; return pt;
} }

View file

@ -12,6 +12,7 @@ _stringnwidth(Font *f, char *s, Rune *r, int len)
Rune rune, **rptr; Rune rune, **rptr;
char *subfontname, **sptr; char *subfontname, **sptr;
Font *def; Font *def;
Subfont *sf;
if(s == nil){ if(s == nil){
s = ""; s = "";
@ -23,6 +24,7 @@ _stringnwidth(Font *f, char *s, Rune *r, int len)
rptr = nil; rptr = nil;
}else }else
rptr = &r; rptr = &r;
sf = nil;
twid = 0; twid = 0;
while(len>0 && (*s || *r)){ while(len>0 && (*s || *r)){
max = Max; max = Max;
@ -43,7 +45,8 @@ _stringnwidth(Font *f, char *s, Rune *r, int len)
return twid; return twid;
} }
if(subfontname){ if(subfontname){
if(_getsubfont(f->display, subfontname) == 0){ freesubfont(sf);
if((sf=_getsubfont(f->display, subfontname)) == 0){
def = f->display->defaultfont; def = f->display->defaultfont;
if(def && f!=def) if(def && f!=def)
f = def; f = def;
@ -56,6 +59,7 @@ _stringnwidth(Font *f, char *s, Rune *r, int len)
twid += wid; twid += wid;
len -= l; len -= l;
} }
freesubfont(sf);
return twid; return twid;
} }

View file

@ -20,8 +20,7 @@ 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(lookupsubfont(i->display, name) == 0) installsubfont(name, f);
installsubfont(name, f);
}else }else
f->name = 0; f->name = 0;
return f; return f;

View file

@ -7,7 +7,7 @@
*/ */
static char *lastname; static char *lastname;
Subfont *lastsubfont; static Subfont *lastsubfont;
Subfont* Subfont*
lookupsubfont(Display *d, char *name) lookupsubfont(Display *d, char *name)