]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccfg.c
Fix string memory leak (Coverity defect #1823).
[fontconfig.git] / src / fccfg.c
index aaca6efc75652446ace1edda21bef51acde3a1c4..fc70fefd09af5fcefbd0e1408d61a0857e6df930 100644 (file)
@@ -22,6 +22,8 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <dirent.h>
+#include <sys/types.h>
 #include "fcint.h"
 
 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
@@ -139,11 +141,6 @@ bail0:
     return 0;
 }
 
-typedef struct _FcFileTime {
-    time_t  time;
-    FcBool  set;
-} FcFileTime;
-
 static FcFileTime
 FcConfigNewestFile (FcStrSet *files)
 {
@@ -166,6 +163,19 @@ FcConfigNewestFile (FcStrSet *files)
     return newest;
 }
 
+FcFileTime
+FcConfigModifiedTime (FcConfig *config)
+{
+    if (!config)
+    {
+       FcFileTime v = { 0, FcFalse };
+       config = FcConfigGetCurrent ();
+       if (!config)
+           return v;
+    }
+    return FcConfigNewestFile (config->configFiles);
+}
+
 FcBool
 FcConfigUptoDate (FcConfig *config)
 {
@@ -266,7 +276,7 @@ FcConfigBuildFonts (FcConfig *config)
         goto bail2;
 
     if (config->cache)
-       FcGlobalCacheLoad (cache, oldDirs, config->cache);
+       FcGlobalCacheLoad (cache, oldDirs, config->cache, config);
 
     cached_fonts = FcCacheRead(config, cache);
     if (!cached_fonts)
@@ -292,7 +302,7 @@ FcConfigBuildFonts (FcConfig *config)
         for (i = 0; i < oldDirs->num; i++)
         {
            if (FcDebug () & FC_DBG_FONTSET)
-               printf ("scan dir %s\n", dir);
+               printf ("scan dir %s\n", oldDirs->strs[i]);
            FcDirScanConfig (fonts, config->fontDirs, cache, 
                             config->blanks, oldDirs->strs[i], 
                              FcFalse, config);
@@ -300,7 +310,11 @@ FcConfigBuildFonts (FcConfig *config)
 
        for (i = 0; i < cached_fonts->nfont; i++)
        {
-           if (FcConfigAcceptFont (config, cached_fonts->fonts[i]))
+           FcChar8     *cfn; 
+           FcPatternGetString (cached_fonts->fonts[i], FC_FILE, 0, &cfn);
+
+           if (FcConfigAcceptFont (config, cached_fonts->fonts[i]) &&
+                (cfn && FcConfigAcceptFilename (config, cfn)))
                FcFontSetAdd (fonts, cached_fonts->fonts[i]);
 
            cached_fonts->fonts[i] = 0; /* prevent free in FcFontSetDestroy */
@@ -313,7 +327,7 @@ FcConfigBuildFonts (FcConfig *config)
        FcFontSetPrint (fonts);
 
     if (config->cache)
-       FcGlobalCacheSave (cache, config->cache);
+       FcGlobalCacheSave (cache, config->cache, config);
     FcGlobalCacheDestroy (cache);
     FcStrSetDestroy (oldDirs);
 
@@ -321,6 +335,7 @@ FcConfigBuildFonts (FcConfig *config)
     
     return FcTrue;
 bail2:
+    FcGlobalCacheDestroy (cache);
     FcStrSetDestroy (oldDirs);
 bail1:
     FcFontSetDestroy (fonts);
@@ -369,13 +384,113 @@ FcConfigGetConfigDirs (FcConfig   *config)
     return FcStrListCreate (config->configDirs);
 }
 
+static FcChar8 *
+FcConfigInodeMatchFontDir (FcConfig *config, const FcChar8 *d)
+{
+    int                n;
+    ino_t      di;
+    dev_t      dd;
+    struct stat s;
+
+    /* first we do string matches rather than file accesses */
+    /* FcStrSetMember doesn't tell us the index so that we can return
+     * the config-owned copy. */
+    for (n = 0; n < config->fontDirs->num; n++)
+    {
+       if (!FcStrCmp (config->fontDirs->strs[n], d))
+           return config->fontDirs->strs[n];
+    }
+
+    /* If this is a bottleneck, we can cache the fontDir inodes. */
+    if (stat ((char *)d, &s) == -1)
+       return 0;
+    di = s.st_ino; dd = s.st_dev;
+
+    for (n = 0; n < config->fontDirs->num; n++)
+    {
+       if (stat ((char *)config->fontDirs->strs[n], &s) == -1)
+           continue;
+       if (di == s.st_ino && dd == s.st_dev)
+           return config->fontDirs->strs[n];
+    }
+    return 0;
+}
+
 FcBool
 FcConfigAddFontDir (FcConfig       *config,
                    const FcChar8   *d)
 {
+    /* Avoid adding d if it's an alias of something else, too. */
+    if (FcConfigInodeMatchFontDir(config, d))
+       return FcTrue;
     return FcStrSetAddFilename (config->fontDirs, d);
 }
 
+static FcBool
+FcConfigAddFontDirSubdirs (FcConfig        *config,
+                          const FcChar8   *d)
+{
+    DIR *dir;
+    struct dirent *e;
+    FcChar8 *subdir;
+    FcBool added = FcFalse;
+    
+    if (!(dir = opendir ((char *) d)))
+       return FcFalse;
+    if (!(subdir = (FcChar8 *) malloc (strlen ((char *) d) + FC_MAX_FILE_LEN + 2)))
+    {
+       fprintf (stderr, "out of memory");
+       return FcFalse;
+    }
+    while ((e = readdir (dir)))
+    {
+       if (strcmp (e->d_name, ".") && strcmp (e->d_name, "..") &&
+           strlen (e->d_name) < FC_MAX_FILE_LEN)
+       {
+           strcpy ((char *)subdir, (char *)d);
+           strcat ((char *)subdir, "/");
+           strcat ((char *)subdir, e->d_name);
+           if (FcFileIsDir (subdir))
+           {
+               if (FcConfigInodeMatchFontDir(config, subdir))
+                   continue; /* already added */
+               FcStrSetAddFilename (config->fontDirs, subdir);
+               FcConfigAddFontDirSubdirs (config, subdir);
+               added = FcTrue;
+           }
+       }
+    }
+    free (subdir);
+    closedir (dir);
+    return added;
+}
+
+const FcChar8 *
+FcConfigNormalizeFontDir (FcConfig     *config, 
+                         const FcChar8 *d)
+{
+    FcChar8    *d0;
+    int                n, n0;
+    FcBool     added = FcFalse;
+
+    d0 = FcConfigInodeMatchFontDir(config, d);
+    if (d0)
+       return d0;
+
+    /* Ok, we didn't find it in fontDirs; let's add subdirs.... */
+    for (n = 0, n0 = config->fontDirs->num; n < n0; n++) 
+    {
+       if (FcConfigAddFontDirSubdirs (config, config->fontDirs->strs[n]))
+           added = FcTrue;
+    }
+
+    /* ... and try again. */
+    if (added)
+       return FcConfigInodeMatchFontDir(config, d);
+
+    return 0;
+}
+
 FcBool
 FcConfigAddDir (FcConfig           *config,
                const FcChar8       *d)
@@ -778,6 +893,7 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
     FcValue    v, vl, vr;
     FcResult   r;
     FcMatrix   *m;
+    FcChar8     *str;
     
     switch (e->op) {
     case FcOpInteger:
@@ -791,7 +907,6 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
     case FcOpString:
        v.type = FcTypeString;
        v.u.s = FcStrStaticName(e->u.sval);
-       v = FcValueSave (v);
        break;
     case FcOpMatrix:
        v.type = FcTypeMatrix;
@@ -811,6 +926,7 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
        r = FcPatternGet (p, e->u.field, 0, &v);
        if (r != FcResultMatch)
            v.type = FcTypeVoid;
+       v = FcValueSave (v);
        break;
     case FcOpConst:
        if (FcNameConstant (e->u.constant, &v.u.i))
@@ -908,7 +1024,9 @@ FcConfigEvaluate (FcPattern *p, FcExpr *e)
                switch (e->op) {
                case FcOpPlus:
                    v.type = FcTypeString;
-                   v.u.s = FcStrStaticName (FcStrPlus (vl.u.s, vr.u.s));
+                   str = FcStrPlus (vl.u.s, vr.u.s);
+                   v.u.s = FcStrStaticName (str);
+                   FcStrFree (str);
                         
                    if (!v.u.s)
                        v.type = FcTypeVoid;
@@ -1083,7 +1201,6 @@ static FcValueList *
 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
 {
     FcValueList        *l;
-    FcValueListPtr lp;
     
     if (!e)
        return 0;
@@ -1102,18 +1219,15 @@ FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
        l->next  = FcValueListPtrCreateDynamic(0);
     }
     l->binding = binding;
-    lp = FcValueListPtrCreateDynamic(l);
-    while (FcValueListPtrU(lp) && FcValueListPtrU(lp)->value.type == FcTypeVoid)
+    if (l->value.type == FcTypeVoid)
     {
-       FcValueListPtr  next = FcValueListPtrU(lp)->next;
+       FcValueList  *next = FcValueListPtrU(l->next);
 
-       if (lp.bank == FC_BANK_DYNAMIC)
-       {
-           FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
-           free (l);
-       }
-       lp = next;
+       FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
+       free (l);
+       l = next;
     }
+
     return l;
 }
 
@@ -1765,6 +1879,7 @@ FcConfigAppFontAddFile (FcConfig    *config,
        }
        FcStrListDone (sublist);
     }
+    FcStrSetDestroy (subdirs);
     return FcTrue;
 }
 
@@ -1812,6 +1927,7 @@ FcConfigAppFontAddDir (FcConfig       *config,
        }
        FcStrListDone (sublist);
     }
+    FcStrSetDestroy (subdirs);
     return FcTrue;
 }