libdraw: fix old subfont leak

front
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);
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;
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);

View File

@ -58,7 +58,6 @@ creadimage(Display *d, int fd, int dolock)
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 @@ creadimage(Display *d, int fd, int dolock)
if(i == nil)
return nil;
}
setmalloctag(i, getcallerpc(&d));
ncblock = _compblocksize(r, chantodepth(chan));
buf = malloc(ncblock);
if(buf == nil)

View File

@ -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);

View File

@ -13,7 +13,6 @@ _getsubfont(Display *d, char *name)
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 @@ _getsubfont(Display *d, char *name)
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;
}

View File

@ -19,8 +19,12 @@ readimage(Display *d, int fd, int dolock)
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 @@ readimage(Display *d, int fd, int dolock)
miny += dy;
}
free(tmp);
Done:
setmalloctag(i, getcallerpc(&d));
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
break;
}
/*
* must not free sf until cachechars has found it in the cache
* and picked up its own reference.
*/
}
}
freesubfont(sf);
return pt;
}

View File

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

View File

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

View File

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