From dc2a17b95cace1f220292cb6df97ee423d6be229 Mon Sep 17 00:00:00 2001 From: Xiao-Yong Jin Date: Tue, 20 Mar 2018 22:17:57 -0500 Subject: libdraw: fix some memory leaks in font handling --- src/libdraw/buildfont.c | 1 + src/libdraw/freesubfont.c | 1 + src/libdraw/openfont.c | 20 ++++++++++++++------ src/libdraw/subfontcache.c | 1 + src/libdraw/subfontname.c | 1 + 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/libdraw/buildfont.c b/src/libdraw/buildfont.c index d3f2e69e..ed533b14 100644 --- a/src/libdraw/buildfont.c +++ b/src/libdraw/buildfont.c @@ -36,6 +36,7 @@ buildfont(Display *d, char *buf, char *name) if(fnt->name==0 || fnt->cache==0 || fnt->subf==0){ Err2: free(fnt->name); + free(fnt->namespec); free(fnt->cache); free(fnt->subf); free(fnt->sub); diff --git a/src/libdraw/freesubfont.c b/src/libdraw/freesubfont.c index 6830b9dc..16b2ef3e 100644 --- a/src/libdraw/freesubfont.c +++ b/src/libdraw/freesubfont.c @@ -12,6 +12,7 @@ freesubfont(Subfont *f) return; uninstallsubfont(f); free(f->info); /* note: f->info must have been malloc'ed! */ + free(f->name); freeimage(f->bits); free(f); } diff --git a/src/libdraw/openfont.c b/src/libdraw/openfont.c index b97ea22d..2b729d73 100644 --- a/src/libdraw/openfont.c +++ b/src/libdraw/openfont.c @@ -31,7 +31,7 @@ openfont1(Display *d, char *name) { Font *fnt; int fd, i, n, scale; - char *buf, *nambuf, *fname, *freename; + char *buf, *nambuf, *nambuf0, *fname, *freename; nambuf = 0; freename = nil; @@ -42,19 +42,21 @@ openfont1(Display *d, char *name) nambuf = smprint("#9/font/%s", fname+14); if(nambuf == nil) return 0; - nambuf = unsharp(nambuf); + *nambuf0 = unsharp(nambuf); + if(nambuf0 != nambuf) + free(nambuf); + nambuf = nambuf0; if(nambuf == nil) return 0; if((fd = open(nambuf, OREAD)) < 0){ free(nambuf); return 0; } - fname = nambuf; if(scale > 1) { - name = smprint("%d*%s", scale, fname); + name = smprint("%d*%s", scale, nambuf); freename = name; } else { - name = fname; + name = nambuf; } } if(fd >= 0) @@ -63,13 +65,17 @@ openfont1(Display *d, char *name) fd = _fontpipe(fname+10); n = 128*1024; } - if(fd < 0) + if(fd < 0){ + free(nambuf); + free(freename); return 0; + } buf = malloc(n+1); if(buf == 0){ close(fd); free(nambuf); + free(freename); return 0; } i = readn(fd, buf, n); @@ -77,6 +83,7 @@ openfont1(Display *d, char *name) if(i <= 0){ free(buf); free(nambuf); + free(freename); return 0; } buf[i] = 0; @@ -224,6 +231,7 @@ openfont(Display *d, char *name) if(!f) return nil; f->lodpi = f; + free(f->namespec); f->namespec = namespec; /* add to display list for when dpi changes */ diff --git a/src/libdraw/subfontcache.c b/src/libdraw/subfontcache.c index 2a7f489b..91a6861a 100644 --- a/src/libdraw/subfontcache.c +++ b/src/libdraw/subfontcache.c @@ -34,6 +34,7 @@ void uninstallsubfont(Subfont *subfont) { if(subfont == lastsubfont){ + free(lastname); lastname = 0; lastsubfont = 0; } diff --git a/src/libdraw/subfontname.c b/src/libdraw/subfontname.c index 9280244a..9d68570d 100644 --- a/src/libdraw/subfontname.c +++ b/src/libdraw/subfontname.c @@ -47,6 +47,7 @@ subfontname(char *cfname, char *fname, int maxdepth) } return tmp2; } + free(tmp2); } /* try default */ -- cgit v1.2.3