tlibdraw: fix some memory leaks in font handling - plan9port - [fork] Plan 9 from user space
git clone git://src.adamsgaard.dk/plan9port
Log
Files
Refs
README
LICENSE
---
commit dc2a17b95cace1f220292cb6df97ee423d6be229
parent a3ec102dc7fee3d4ecb32ade5ec9d7de4b9b4304
Author: Xiao-Yong Jin 
Date:   Tue, 20 Mar 2018 22:17:57 -0500

libdraw: fix some memory leaks in font handling

Diffstat:
  M src/libdraw/buildfont.c             |       1 +
  M src/libdraw/freesubfont.c           |       1 +
  M src/libdraw/openfont.c              |      20 ++++++++++++++------
  M src/libdraw/subfontcache.c          |       1 +
  M src/libdraw/subfontname.c           |       1 +

5 files changed, 18 insertions(+), 6 deletions(-)
---
diff --git a/src/libdraw/buildfont.c b/src/libdraw/buildfont.c
t@@ -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
t@@ -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
t@@ -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;
t@@ -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)
t@@ -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);
t@@ -77,6 +83,7 @@ openfont1(Display *d, char *name)
         if(i <= 0){
                 free(buf);
                 free(nambuf);
+                free(freename);
                 return 0;
         }
         buf[i] = 0;
t@@ -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
t@@ -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
t@@ -47,6 +47,7 @@ subfontname(char *cfname, char *fname, int maxdepth)
                         }
                         return tmp2;
                 }
+                free(tmp2);
         }
 
         /* try default */