]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccache.c
Make fccache more resilient to broken cache files by checking return value
[fontconfig.git] / src / fccache.c
index 349bc9ab500a69a12e32d5b107af1afbca873417..5af247a13ff23b9ce9e9185f4b45dfebdff81fd9 100644 (file)
@@ -157,6 +157,7 @@ FcCacheWriteString (int fd, const char *chars)
 static void
 FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
 {
+    FcStrSetDestroy (d->subdirs);
     FcMemFree (FC_MEM_STRING, strlen (d->name)+1);
     free (d->name);
     FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir));
@@ -206,6 +207,7 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
     off_t              current_arch_start;
 
     struct stat        cache_stat, dir_stat;
+    char               subdirName[FC_MAX_FILE_LEN + 1 + 12 + 1];
 
     if (stat ((char *) cache_file, &cache_stat) < 0)
         return;
@@ -216,7 +218,8 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
 
     cache->updated = FcFalse;
 
-    FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
+    if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)))
+       return;
     if (strcmp (name_buf, FC_GLOBAL_MAGIC_COOKIE) != 0)
        return;
 
@@ -227,8 +230,9 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
         goto bail_and_destroy;
 
     lseek (cache->fd, current_arch_start, SEEK_SET);
-    FcCacheReadString (cache->fd, candidate_arch_machine_name, 
-                       sizeof (candidate_arch_machine_name));
+    if (!FcCacheReadString (cache->fd, candidate_arch_machine_name, 
+                           sizeof (candidate_arch_machine_name)))
+       goto bail_and_destroy;
     if (strlen(candidate_arch_machine_name) == 0)
        goto bail_and_destroy;
 
@@ -236,8 +240,7 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
     {
        off_t targ;
 
-       FcCacheReadString (cache->fd, name_buf, sizeof (name_buf));
-       if (!strlen(name_buf))
+       if (!FcCacheReadString (cache->fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
            break;
 
        /* Directory must be older than the global cache file; also
@@ -264,6 +267,11 @@ FcGlobalCacheLoad (FcGlobalCache    *cache,
        d->name = (char *)FcStrCopy ((FcChar8 *)name_buf);
        d->ent = 0;
        d->offset = lseek (cache->fd, 0, SEEK_CUR);
+
+       d->subdirs = FcStrSetCreate();
+       while (strlen(FcCacheReadString (cache->fd, subdirName, sizeof (subdirName))) > 0)
+           FcStrSetAdd (d->subdirs, (FcChar8 *)subdirName);
+
        if (read (cache->fd, &d->metadata, sizeof (FcCache)) != sizeof (FcCache))
            goto bail1;
        targ = FcCacheNextOffset (lseek(cache->fd, 0, SEEK_CUR)) + d->metadata.count;
@@ -304,13 +312,15 @@ FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, con
     if (cache->fd == -1)
        return FcFalse;
 
-    dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir);
+    if (!(dir = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)dir)))
+       return FcFalse; /* non-existing directory */
+
     for (d = cache->dirs; d; d = d->next)
     {
        if (strncmp (d->name, dir, strlen(dir)) == 0)
        {
            lseek (cache->fd, d->offset, SEEK_SET);
-           if (!FcDirCacheConsume (cache->fd, dir, set, config))
+           if (!FcDirCacheConsume (cache->fd, d->name, set, config))
                return FcFalse;
             if (strcmp (d->name, dir) == 0)
                ret = FcTrue;
@@ -322,11 +332,13 @@ FcGlobalCacheReadDir (FcFontSet *set, FcStrSet *dirs, FcGlobalCache * cache, con
 
 FcBool
 FcGlobalCacheUpdate (FcGlobalCache  *cache,
+                    FcStrSet       *dirs,
                     const char     *name,
                     FcFontSet      *set,
                     FcConfig       *config)
 {
-    FcGlobalCacheDir * d;
+    FcGlobalCacheDir    *d;
+    int                        i;
 
     name = (char *)FcConfigNormalizeFontDir (config, (FcChar8 *)name);
     for (d = cache->dirs; d; d = d->next)
@@ -349,6 +361,9 @@ FcGlobalCacheUpdate (FcGlobalCache  *cache,
     d->name = (char *)FcStrCopy ((FcChar8 *)name);
     d->ent = FcDirCacheProduce (set, &d->metadata);
     d->offset = 0;
+    d->subdirs = FcStrSetCreate();
+    for (i = 0; i < dirs->num; i++)
+       FcStrSetAdd (d->subdirs, dirs->strs[i]);
     return FcTrue;
 }
 
@@ -357,7 +372,7 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
                   const FcChar8    *cache_file,
                   FcConfig         *config)
 {
-    int                        fd, fd_orig;
+    int                        fd, fd_orig, i;
     FcGlobalCacheDir   *dir;
     FcAtomic           *atomic;
     off_t              current_arch_start = 0, truncate_to;
@@ -422,6 +437,10 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
        truncate_to += sizeof (FcCache);
        truncate_to = FcCacheNextOffset (truncate_to);
        truncate_to += dir->metadata.count;
+
+       for (i = 0; i < dir->subdirs->size; i++)
+           truncate_to += strlen((char *)dir->subdirs->strs[i]) + 1;
+       truncate_to ++;
     }
     truncate_to -= current_arch_start;
 
@@ -437,6 +456,11 @@ FcGlobalCacheSave (FcGlobalCache    *cache,
            const char * d = (const char *)FcConfigNormalizeFontDir (config, (const FcChar8 *)dir->name);
 
             FcCacheWriteString (fd, d);
+
+           for (i = 0; i < dir->subdirs->size; i++)
+               FcCacheWriteString (fd, (char *)dir->subdirs->strs[i]);
+           FcCacheWriteString (fd, "");
+           
             write (fd, &dir->metadata, sizeof(FcCache));
             lseek (fd, FcCacheNextOffset (lseek(fd, 0, SEEK_CUR)), SEEK_SET);
             write (fd, dir->ent, dir->metadata.count);
@@ -698,8 +722,7 @@ FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
            return FcTrue;
        }
 
-       FcCacheReadString (fd, name_buf, sizeof (name_buf));
-       if (!strlen(name_buf))
+       if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
        {
            FcStrFree ((FcChar8 *)cache_hashed);
            goto bail;
@@ -920,8 +943,7 @@ FcDirCacheOpen (const FcChar8 *dir)
            FcStrFree ((FcChar8 *)cache_file);
            return -1;
        }
-       FcCacheReadString (fd, name_buf, sizeof (name_buf));
-       if (!strlen(name_buf))
+       if (!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
            goto bail;
 
        name_buf_dir = FcStrDirname ((FcChar8 *)name_buf);
@@ -967,7 +989,7 @@ FcDirCacheRead (FcFontSet * set, FcStrSet * dirs, const FcChar8 *dir, FcConfig *
                           sizeof (candidate_arch_machine_name)) == 0)
        goto bail1;
 
-    while (strlen(FcCacheReadString (fd, subdirName, sizeof (subdirName))) > 0)
+    while (FcCacheReadString (fd, subdirName, sizeof (subdirName)) && strlen (subdirName) > 0)
         FcStrSetAdd (dirs, (FcChar8 *)subdirName);
 
     if (!FcDirCacheConsume (fd, (const char *)dir, set, config))
@@ -1107,11 +1129,12 @@ FcDirCacheWrite (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
        fd = open(cache_hashed, O_RDONLY);
        if (fd == -1)
            break;
-       FcCacheReadString (fd, name_buf, sizeof (name_buf));
-       close (fd);
-
-       if (!strlen(name_buf))
+       if(!FcCacheReadString (fd, name_buf, sizeof (name_buf)) || !strlen(name_buf))
+       {
+           close (fd);
            break;
+       }
+       close (fd);
 
        if (strcmp (name_buf, cache_file) != 0)
            continue;