Use FcConfigReference/Destroy appropriately instead of FcConfigGetCurrent

This may improves to be MT-safe.

Reported at https://bugs.chromium.org/p/chromium/issues/detail?id=1004254
This commit is contained in:
Akira TAGOH 2019-11-01 14:43:42 +09:00
parent a45fc8a332
commit b5bcf61fe7
11 changed files with 436 additions and 153 deletions

View File

@ -174,6 +174,10 @@ Returns one of the two sets of fonts from the configuration as specified
by <parameter>set</parameter>. This font set is owned by the library and must by <parameter>set</parameter>. This font set is owned by the library and must
not be modified or freed. not be modified or freed.
If <parameter>config</parameter> is NULL, the current configuration is used. If <parameter>config</parameter> is NULL, the current configuration is used.
</para><para>
This function isn't MT-safe. <function>FcConfigReference</function> must be called
before using this and then <function>FcConfigDestroy</function> when
the return value is no longer referenced.
@@ @@
@RET@ FcBlanks * @RET@ FcBlanks *
@ -407,6 +411,10 @@ parse error, semantic error or allocation failure. Otherwise returns FcTrue.
Obtains the system root directory in 'config' if available. All files Obtains the system root directory in 'config' if available. All files
(including file properties in patterns) obtained from this 'config' are (including file properties in patterns) obtained from this 'config' are
relative to this system root directory. relative to this system root directory.
</para><para>
This function isn't MT-safe. <function>FcConfigReference</function> must be called
before using this and then <function>FcConfigDestroy</function> when
the return value is no longer referenced.
@SINCE@ 2.10.92 @SINCE@ 2.10.92
@@ @@
@ -433,6 +441,10 @@ When setting this on the current config this causes changing current config
@PURPOSE@ Initialize the iterator @PURPOSE@ Initialize the iterator
@DESC@ @DESC@
Initialize 'iter' with the first iterator in the config file information list. Initialize 'iter' with the first iterator in the config file information list.
</para><para>
This function isn't MT-safe. <function>FcConfigReference</function> must be called
before using this and then <function>FcConfigDestroy</function> when the relevant
values are no longer referenced.
@SINCE@ 2.12.91 @SINCE@ 2.12.91
@@ @@
@ -444,6 +456,10 @@ Initialize 'iter' with the first iterator in the config file information list.
@DESC@ @DESC@
Set 'iter' to point to the next node in the config file information list. Set 'iter' to point to the next node in the config file information list.
If there is no next node, FcFalse is returned. If there is no next node, FcFalse is returned.
</para><para>
This function isn't MT-safe. <function>FcConfigReference</function> must be called
before using <function>FcConfigFileInfoIterInit</function> and then
<function>FcConfigDestroy</function> when the relevant values are no longer referenced.
@SINCE@ 2.12.91 @SINCE@ 2.12.91
@@ @@
@ -459,5 +475,9 @@ If there is no next node, FcFalse is returned.
Obtain the filename, the description and the flag whether it is enabled or not Obtain the filename, the description and the flag whether it is enabled or not
for 'iter' where points to current configuration file information. for 'iter' where points to current configuration file information.
If the iterator is invalid, FcFalse is returned. If the iterator is invalid, FcFalse is returned.
</para><para>
This function isn't MT-safe. <function>FcConfigReference</function> must be called
before using <function>FcConfigFileInfoIterInit</function> and then
<function>FcConfigDestroy</function> when the relevant values are no longer referenced.
@SINCE@ 2.12.91 @SINCE@ 2.12.91
@@ @@

View File

@ -375,7 +375,7 @@ FcPublic FcBool
FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose); FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose);
FcPublic void FcPublic void
FcCacheCreateTagFile (const FcConfig *config); FcCacheCreateTagFile (FcConfig *config);
FcPublic FcBool FcPublic FcBool
FcDirCacheCreateUUID (FcChar8 *dir, FcDirCacheCreateUUID (FcChar8 *dir,
@ -437,7 +437,7 @@ FcPublic FcBlanks *
FcConfigGetBlanks (FcConfig *config); FcConfigGetBlanks (FcConfig *config);
FcPublic FcStrList * FcPublic FcStrList *
FcConfigGetCacheDirs (const FcConfig *config); FcConfigGetCacheDirs (FcConfig *config);
FcPublic int FcPublic int
FcConfigGetRescanInterval (FcConfig *config); FcConfigGetRescanInterval (FcConfig *config);

View File

@ -58,11 +58,15 @@ FcDirCacheDeleteUUID (const FcChar8 *dir,
{ {
FcBool ret = FcTrue; FcBool ret = FcTrue;
#ifndef _WIN32 #ifndef _WIN32
const FcChar8 *sysroot = FcConfigGetSysRoot (config); const FcChar8 *sysroot;
FcChar8 *target, *d; FcChar8 *target, *d;
struct stat statb; struct stat statb;
struct timeval times[2]; struct timeval times[2];
config = FcConfigReference (config);
if (!config)
return FcFalse;
sysroot = FcConfigGetSysRoot (config);
if (sysroot) if (sysroot)
d = FcStrBuildFilename (sysroot, dir, NULL); d = FcStrBuildFilename (sysroot, dir, NULL);
else else
@ -94,6 +98,7 @@ FcDirCacheDeleteUUID (const FcChar8 *dir,
bail: bail:
FcStrFree (d); FcStrFree (d);
#endif #endif
FcConfigDestroy (config);
return ret; return ret;
} }
@ -265,7 +270,13 @@ FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
#endif #endif
FcStrList *list; FcStrList *list;
FcChar8 *cache_dir; FcChar8 *cache_dir;
const FcChar8 *sysroot = FcConfigGetSysRoot (config); const FcChar8 *sysroot;
FcBool ret = FcTrue;
config = FcConfigReference (config);
if (!config)
return FcFalse;
sysroot = FcConfigGetSysRoot (config);
FcDirCacheBasenameMD5 (config, dir, cache_base); FcDirCacheBasenameMD5 (config, dir, cache_base);
#ifndef _WIN32 #ifndef _WIN32
@ -274,7 +285,10 @@ FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
list = FcStrListCreate (config->cacheDirs); list = FcStrListCreate (config->cacheDirs);
if (!list) if (!list)
return FcFalse; {
ret = FcFalse;
goto bail;
}
while ((cache_dir = FcStrListNext (list))) while ((cache_dir = FcStrListNext (list)))
{ {
@ -304,8 +318,11 @@ FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
FcDirCacheDeleteUUID (dir, config); FcDirCacheDeleteUUID (dir, config);
/* return FcFalse if something went wrong */ /* return FcFalse if something went wrong */
if (cache_dir) if (cache_dir)
return FcFalse; ret = FcFalse;
return FcTrue; bail:
FcConfigDestroy (config);
return ret;
} }
static int static int
@ -1041,10 +1058,15 @@ FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
{ {
FcCache *cache = NULL; FcCache *cache = NULL;
config = FcConfigReference (config);
if (!config)
return NULL;
if (!FcDirCacheProcess (config, dir, if (!FcDirCacheProcess (config, dir,
FcDirCacheMapHelper, FcDirCacheMapHelper,
&cache, cache_file)) &cache, cache_file))
return NULL; cache = NULL;
FcConfigDestroy (config);
return cache; return cache;
} }
@ -1055,13 +1077,16 @@ FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat)
int fd; int fd;
FcCache *cache; FcCache *cache;
struct stat my_file_stat; struct stat my_file_stat;
FcConfig *config;
if (!file_stat) if (!file_stat)
file_stat = &my_file_stat; file_stat = &my_file_stat;
fd = FcDirCacheOpenFile (cache_file, file_stat); fd = FcDirCacheOpenFile (cache_file, file_stat);
if (fd < 0) if (fd < 0)
return NULL; return NULL;
cache = FcDirCacheMapFd (FcConfigGetCurrent (), fd, file_stat, NULL); config = FcConfigReference (NULL);
cache = FcDirCacheMapFd (config, fd, file_stat, NULL);
FcConfigDestroy (config);
close (fd); close (fd);
return cache; return cache;
} }
@ -1155,12 +1180,16 @@ FcBool
FcDirCacheValid (const FcChar8 *dir) FcDirCacheValid (const FcChar8 *dir)
{ {
FcConfig *config; FcConfig *config;
FcBool ret;
config = FcConfigGetCurrent (); config = FcConfigReference (NULL);
if (!config) if (!config)
return FcFalse; return FcFalse;
return FcDirCacheValidConfig (dir, config); ret = FcDirCacheValidConfig (dir, config);
FcConfigDestroy (config);
return ret;
} }
/* /*
@ -1438,9 +1467,13 @@ FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
FcCache *cache; FcCache *cache;
struct stat target_stat; struct stat target_stat;
const FcChar8 *sysroot; const FcChar8 *sysroot;
FcConfig *config;
config = FcConfigReference (NULL);
if (!config)
return FcFalse;
/* FIXME: this API needs to support non-current FcConfig */ /* FIXME: this API needs to support non-current FcConfig */
sysroot = FcConfigGetSysRoot (NULL); sysroot = FcConfigGetSysRoot (config);
if (sysroot) if (sysroot)
dir = FcStrBuildFilename (sysroot, cache_dir, NULL); dir = FcStrBuildFilename (sysroot, cache_dir, NULL);
else else
@ -1448,7 +1481,8 @@ FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
if (!dir) if (!dir)
{ {
fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir); fprintf (stderr, "Fontconfig error: %s: out of memory\n", cache_dir);
return FcFalse; ret = FcFalse;
goto bail;
} }
if (access ((char *) dir, W_OK) != 0) if (access ((char *) dir, W_OK) != 0)
{ {
@ -1525,8 +1559,10 @@ FcDirCacheClean (const FcChar8 *cache_dir, FcBool verbose)
} }
closedir (d); closedir (d);
bail0: bail0:
FcStrFree (dir); FcStrFree (dir);
bail:
FcConfigDestroy (config);
return ret; return ret;
} }
@ -1968,15 +2004,20 @@ FcDirCacheCreateTagFile (const FcChar8 *cache_dir)
} }
void void
FcCacheCreateTagFile (const FcConfig *config) FcCacheCreateTagFile (FcConfig *config)
{ {
FcChar8 *cache_dir = NULL, *d = NULL; FcChar8 *cache_dir = NULL, *d = NULL;
FcStrList *list; FcStrList *list;
const FcChar8 *sysroot = FcConfigGetSysRoot (config); const FcChar8 *sysroot;
config = FcConfigReference (config);
if (!config)
return;
sysroot = FcConfigGetSysRoot (config);
list = FcConfigGetCacheDirs (config); list = FcConfigGetCacheDirs (config);
if (!list) if (!list)
return; goto bail;
while ((cache_dir = FcStrListNext (list))) while ((cache_dir = FcStrListNext (list)))
{ {
@ -1992,6 +2033,8 @@ FcCacheCreateTagFile (const FcConfig *config)
if (d) if (d)
FcStrFree (d); FcStrFree (d);
FcStrListDone (list); FcStrListDone (list);
bail:
FcConfigDestroy (config);
} }
#define __fccache__ #define __fccache__

View File

@ -237,12 +237,12 @@ FcConfigUptoDate (FcConfig *config)
{ {
FcFileTime config_time, config_dir_time, font_time; FcFileTime config_time, config_dir_time, font_time;
time_t now = time(0); time_t now = time(0);
if (!config) FcBool ret = FcTrue;
{
config = FcConfigGetCurrent (); config = FcConfigReference (config);
if (!config) if (!config)
return FcFalse; return FcFalse;
}
config_time = FcConfigNewestFile (config->configFiles); config_time = FcConfigNewestFile (config->configFiles);
config_dir_time = FcConfigNewestFile (config->configDirs); config_dir_time = FcConfigNewestFile (config->configDirs);
font_time = FcConfigNewestFile (config->fontDirs); font_time = FcConfigNewestFile (config->fontDirs);
@ -258,13 +258,19 @@ FcConfigUptoDate (FcConfig *config)
fprintf (stderr, fprintf (stderr,
"Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected.\n"); "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected.\n");
config->rescanTime = now; config->rescanTime = now;
return FcTrue; goto bail;
} }
else else
return FcFalse; {
ret = FcFalse;
goto bail;
}
} }
config->rescanTime = now; config->rescanTime = now;
return FcTrue; bail:
FcConfigDestroy (config);
return ret;
} }
FcExpr * FcExpr *
@ -291,11 +297,26 @@ FcConfigReference (FcConfig *config)
{ {
if (!config) if (!config)
{ {
config = FcConfigGetCurrent (); /* Do not use FcConfigGetCurrent () for the purpose of obtaining current FcConfig here.
* because the reference counter must be increased before setting it to _fcConfig.
*/
retry:
config = fc_atomic_ptr_get (&_fcConfig);
if (!config) if (!config)
return 0; {
} config = FcConfigCreate ();
FcRefInc (&config->ref);
config = FcInitLoadOwnConfigAndFonts (config);
if (!fc_atomic_ptr_cmpexch (&_fcConfig, NULL, config))
{
FcConfigDestroy (config); /* To decrease the refcount for the above one. */
FcConfigDestroy (config); /* To destroy it actualy */
goto retry;
}
return config;
}
}
FcRefInc (&config->ref); FcRefInc (&config->ref);
return config; return config;
@ -475,25 +496,32 @@ FcBool
FcConfigBuildFonts (FcConfig *config) FcConfigBuildFonts (FcConfig *config)
{ {
FcFontSet *fonts; FcFontSet *fonts;
FcBool ret = FcTrue;
if (!config) config = FcConfigReference (config);
{
config = FcConfigGetCurrent ();
if (!config) if (!config)
return FcFalse; return FcFalse;
}
fonts = FcFontSetCreate (); fonts = FcFontSetCreate ();
if (!fonts) if (!fonts)
return FcFalse; {
ret = FcFalse;
goto bail;
}
FcConfigSetFonts (config, fonts, FcSetSystem); FcConfigSetFonts (config, fonts, FcSetSystem);
if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs)) if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs))
return FcFalse; {
ret = FcFalse;
goto bail;
}
if (FcDebug () & FC_DBG_FONTSET) if (FcDebug () & FC_DBG_FONTSET)
FcFontSetPrint (fonts); FcFontSetPrint (fonts);
return FcTrue; bail:
FcConfigDestroy (config);
return ret;
} }
FcBool FcBool
@ -537,13 +565,15 @@ FcConfigAddConfigDir (FcConfig *config,
FcStrList * FcStrList *
FcConfigGetConfigDirs (FcConfig *config) FcConfigGetConfigDirs (FcConfig *config)
{ {
FcStrList *ret;
config = FcConfigReference (config);
if (!config) if (!config)
{ return NULL;
config = FcConfigGetCurrent (); ret = FcStrListCreate (config->configDirs);
if (!config) FcConfigDestroy (config);
return 0;
} return ret;
return FcStrListCreate (config->configDirs);
} }
FcBool FcBool
@ -579,13 +609,15 @@ FcConfigResetFontDirs (FcConfig *config)
FcStrList * FcStrList *
FcConfigGetFontDirs (FcConfig *config) FcConfigGetFontDirs (FcConfig *config)
{ {
FcStrList *ret;
config = FcConfigReference (config);
if (!config) if (!config)
{ return NULL;
config = FcConfigGetCurrent (); ret = FcStrListCreate (config->fontDirs);
if (!config) FcConfigDestroy (config);
return 0;
} return ret;
return FcStrListCreate (config->fontDirs);
} }
static FcBool static FcBool
@ -670,15 +702,17 @@ FcConfigAddCacheDir (FcConfig *config,
} }
FcStrList * FcStrList *
FcConfigGetCacheDirs (const FcConfig *config) FcConfigGetCacheDirs (FcConfig *config)
{ {
FcStrList *ret;
config = FcConfigReference (config);
if (!config) if (!config)
{ return NULL;
config = FcConfigGetCurrent (); ret = FcStrListCreate (config->cacheDirs);
if (!config) FcConfigDestroy (config);
return 0;
} return ret;
return FcStrListCreate (config->cacheDirs);
} }
FcBool FcBool
@ -699,13 +733,15 @@ FcConfigAddConfigFile (FcConfig *config,
FcStrList * FcStrList *
FcConfigGetConfigFiles (FcConfig *config) FcConfigGetConfigFiles (FcConfig *config)
{ {
FcStrList *ret;
config = FcConfigReference (config);
if (!config) if (!config)
{ return NULL;
config = FcConfigGetCurrent (); ret = FcStrListCreate (config->configFiles);
if (!config) FcConfigDestroy (config);
return 0;
} return ret;
return FcStrListCreate (config->configFiles);
} }
FcChar8 * FcChar8 *
@ -784,25 +820,26 @@ FcConfigAddBlank (FcConfig *config FC_UNUSED,
int int
FcConfigGetRescanInterval (FcConfig *config) FcConfigGetRescanInterval (FcConfig *config)
{ {
if (!config) int ret;
{
config = FcConfigGetCurrent (); config = FcConfigReference (config);
if (!config) if (!config)
return 0; return 0;
} ret = config->rescanInterval;
return config->rescanInterval; FcConfigDestroy (config);
return ret;
} }
FcBool FcBool
FcConfigSetRescanInterval (FcConfig *config, int rescanInterval) FcConfigSetRescanInterval (FcConfig *config, int rescanInterval)
{ {
if (!config) config = FcConfigReference (config);
{
config = FcConfigGetCurrent ();
if (!config) if (!config)
return FcFalse; return FcFalse;
}
config->rescanInterval = rescanInterval; config->rescanInterval = rescanInterval;
FcConfigDestroy (config);
return FcTrue; return FcTrue;
} }
@ -1670,15 +1707,13 @@ FcConfigSubstituteWithPat (FcConfig *config,
FcBool retval = FcTrue; FcBool retval = FcTrue;
FcTest **tst = NULL; FcTest **tst = NULL;
if (!config)
{
config = FcConfigGetCurrent ();
if (!config)
return FcFalse;
}
if (kind < FcMatchKindBegin || kind >= FcMatchKindEnd) if (kind < FcMatchKindBegin || kind >= FcMatchKindEnd)
return FcFalse; return FcFalse;
config = FcConfigReference (config);
if (!config)
return FcFalse;
s = config->subst[kind]; s = config->subst[kind];
if (kind == FcMatchPattern) if (kind == FcMatchPattern)
{ {
@ -1973,6 +2008,7 @@ bail1:
free (value); free (value);
if (tst) if (tst)
free (tst); free (tst);
FcConfigDestroy (config);
return retval; return retval;
} }
@ -2290,12 +2326,9 @@ FcConfigGetFilename (FcConfig *config,
FcChar8 *file, *dir, **path, **p; FcChar8 *file, *dir, **path, **p;
const FcChar8 *sysroot; const FcChar8 *sysroot;
if (!config) config = FcConfigReference (config);
{
config = FcConfigGetCurrent ();
if (!config) if (!config)
return NULL; return NULL;
}
sysroot = FcConfigGetSysRoot (config); sysroot = FcConfigGetSysRoot (config);
if (!url || !*url) if (!url || !*url)
{ {
@ -2306,7 +2339,10 @@ FcConfigGetFilename (FcConfig *config,
file = 0; file = 0;
if (FcStrIsAbsoluteFilename(url)) if (FcStrIsAbsoluteFilename(url))
return FcConfigFileExists (sysroot, url); {
file = FcConfigFileExists (sysroot, url);
goto bail;
}
if (*url == '~') if (*url == '~')
{ {
@ -2330,7 +2366,10 @@ FcConfigGetFilename (FcConfig *config,
{ {
path = FcConfigGetPath (); path = FcConfigGetPath ();
if (!path) if (!path)
return NULL; {
file = NULL;
goto bail;
}
for (p = path; *p; p++) for (p = path; *p; p++)
{ {
FcChar8 *s; FcChar8 *s;
@ -2347,6 +2386,9 @@ FcConfigGetFilename (FcConfig *config,
} }
FcConfigFreePath (path); FcConfigFreePath (path);
} }
bail:
FcConfigDestroy (config);
return file; return file;
} }
@ -2409,17 +2451,18 @@ FcConfigAppFontAddFile (FcConfig *config,
FcStrSet *subdirs; FcStrSet *subdirs;
FcStrList *sublist; FcStrList *sublist;
FcChar8 *subdir; FcChar8 *subdir;
FcBool ret = FcTrue;
if (!config) config = FcConfigReference (config);
{
config = FcConfigGetCurrent ();
if (!config) if (!config)
return FcFalse; return FcFalse;
}
subdirs = FcStrSetCreateEx (FCSS_GROW_BY_64); subdirs = FcStrSetCreateEx (FCSS_GROW_BY_64);
if (!subdirs) if (!subdirs)
return FcFalse; {
ret = FcFalse;
goto bail;
}
set = FcConfigGetFonts (config, FcSetApplication); set = FcConfigGetFonts (config, FcSetApplication);
if (!set) if (!set)
@ -2428,7 +2471,8 @@ FcConfigAppFontAddFile (FcConfig *config,
if (!set) if (!set)
{ {
FcStrSetDestroy (subdirs); FcStrSetDestroy (subdirs);
return FcFalse; ret = FcFalse;
goto bail;
} }
FcConfigSetFonts (config, set, FcSetApplication); FcConfigSetFonts (config, set, FcSetApplication);
} }
@ -2436,7 +2480,8 @@ FcConfigAppFontAddFile (FcConfig *config,
if (!FcFileScanConfig (set, subdirs, file, config)) if (!FcFileScanConfig (set, subdirs, file, config))
{ {
FcStrSetDestroy (subdirs); FcStrSetDestroy (subdirs);
return FcFalse; ret = FcFalse;
goto bail;
} }
if ((sublist = FcStrListCreate (subdirs))) if ((sublist = FcStrListCreate (subdirs)))
{ {
@ -2447,7 +2492,10 @@ FcConfigAppFontAddFile (FcConfig *config,
FcStrListDone (sublist); FcStrListDone (sublist);
} }
FcStrSetDestroy (subdirs); FcStrSetDestroy (subdirs);
return FcTrue; bail:
FcConfigDestroy (config);
return ret;
} }
FcBool FcBool
@ -2456,17 +2504,18 @@ FcConfigAppFontAddDir (FcConfig *config,
{ {
FcFontSet *set; FcFontSet *set;
FcStrSet *dirs; FcStrSet *dirs;
FcBool ret = FcTrue;
if (!config) config = FcConfigReference (config);
{
config = FcConfigGetCurrent ();
if (!config) if (!config)
return FcFalse; return FcFalse;
}
dirs = FcStrSetCreateEx (FCSS_GROW_BY_64); dirs = FcStrSetCreateEx (FCSS_GROW_BY_64);
if (!dirs) if (!dirs)
return FcFalse; {
ret = FcFalse;
goto bail;
}
set = FcConfigGetFonts (config, FcSetApplication); set = FcConfigGetFonts (config, FcSetApplication);
if (!set) if (!set)
@ -2475,7 +2524,8 @@ FcConfigAppFontAddDir (FcConfig *config,
if (!set) if (!set)
{ {
FcStrSetDestroy (dirs); FcStrSetDestroy (dirs);
return FcFalse; ret = FcFalse;
goto bail;
} }
FcConfigSetFonts (config, set, FcSetApplication); FcConfigSetFonts (config, set, FcSetApplication);
} }
@ -2485,23 +2535,26 @@ FcConfigAppFontAddDir (FcConfig *config,
if (!FcConfigAddDirList (config, FcSetApplication, dirs)) if (!FcConfigAddDirList (config, FcSetApplication, dirs))
{ {
FcStrSetDestroy (dirs); FcStrSetDestroy (dirs);
return FcFalse; ret = FcFalse;
goto bail;
} }
FcStrSetDestroy (dirs); FcStrSetDestroy (dirs);
return FcTrue; bail:
FcConfigDestroy (config);
return ret;
} }
void void
FcConfigAppFontClear (FcConfig *config) FcConfigAppFontClear (FcConfig *config)
{ {
if (!config) config = FcConfigReference (config);
{
config = FcConfigGetCurrent ();
if (!config) if (!config)
return; return;
}
FcConfigSetFonts (config, 0, FcSetApplication); FcConfigSetFonts (config, 0, FcSetApplication);
FcConfigDestroy (config);
} }
/* /*

View File

@ -167,7 +167,16 @@ FcFileScan (FcFontSet *set,
const FcChar8 *file, const FcChar8 *file,
FcBool force FC_UNUSED) FcBool force FC_UNUSED)
{ {
return FcFileScanConfig (set, dirs, file, FcConfigGetCurrent ()); FcConfig *config;
FcBool ret;
config = FcConfigReference (NULL);
if (!config)
return FcFalse;
ret = FcFileScanConfig (set, dirs, file, config);
FcConfigDestroy (config);
return ret;
} }
/* /*
@ -271,10 +280,19 @@ FcDirScan (FcFontSet *set,
const FcChar8 *dir, const FcChar8 *dir,
FcBool force FC_UNUSED) FcBool force FC_UNUSED)
{ {
FcConfig *config;
FcBool ret;
if (cache || !force) if (cache || !force)
return FcFalse; return FcFalse;
return FcDirScanConfig (set, dirs, dir, force, FcConfigGetCurrent ()); config = FcConfigReference (NULL);
if (!config)
return FcFalse;
ret = FcDirScanConfig (set, dirs, dir, force, config);
FcConfigDestroy (config);
return ret;
} }
/* /*
@ -353,12 +371,16 @@ FcDirCacheRescan (const FcChar8 *dir, FcConfig *config)
FcCache *new = NULL; FcCache *new = NULL;
struct stat dir_stat; struct stat dir_stat;
FcStrSet *dirs; FcStrSet *dirs;
const FcChar8 *sysroot = FcConfigGetSysRoot (config); const FcChar8 *sysroot;
FcChar8 *d = NULL; FcChar8 *d = NULL;
#ifndef _WIN32 #ifndef _WIN32
int fd = -1; int fd = -1;
#endif #endif
config = FcConfigReference (config);
if (!config)
return NULL;
sysroot = FcConfigGetSysRoot (config);
cache = FcDirCacheLoad (dir, config, NULL); cache = FcDirCacheLoad (dir, config, NULL);
if (!cache) if (!cache)
goto bail; goto bail;
@ -401,6 +423,7 @@ bail1:
bail: bail:
if (d) if (d)
FcStrFree (d); FcStrFree (d);
FcConfigDestroy (config);
return new; return new;
} }
@ -413,6 +436,7 @@ FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
{ {
FcCache *cache = NULL; FcCache *cache = NULL;
config = FcConfigReference (config);
/* Try to use existing cache file */ /* Try to use existing cache file */
if (!force) if (!force)
cache = FcDirCacheLoad (dir, config, NULL); cache = FcDirCacheLoad (dir, config, NULL);
@ -420,6 +444,7 @@ FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
/* Not using existing cache file, construct new cache */ /* Not using existing cache file, construct new cache */
if (!cache) if (!cache)
cache = FcDirCacheScan (dir, config); cache = FcDirCacheScan (dir, config);
FcConfigDestroy (config);
return cache; return cache;
} }

View File

@ -229,7 +229,8 @@ FcInitReinitialize (void)
FcBool FcBool
FcInitBringUptoDate (void) FcInitBringUptoDate (void)
{ {
FcConfig *config = FcConfigGetCurrent (); FcConfig *config = FcConfigReference (NULL);
FcBool ret = FcTrue;
time_t now; time_t now;
if (!config) if (!config)
@ -238,19 +239,23 @@ FcInitBringUptoDate (void)
* rescanInterval == 0 disables automatic up to date * rescanInterval == 0 disables automatic up to date
*/ */
if (config->rescanInterval == 0) if (config->rescanInterval == 0)
return FcTrue; goto bail;
/* /*
* Check no more often than rescanInterval seconds * Check no more often than rescanInterval seconds
*/ */
now = time (0); now = time (0);
if (config->rescanTime + config->rescanInterval - now > 0) if (config->rescanTime + config->rescanInterval - now > 0)
return FcTrue; goto bail;
/* /*
* If up to date, don't reload configuration * If up to date, don't reload configuration
*/ */
if (FcConfigUptoDate (0)) if (FcConfigUptoDate (0))
return FcTrue; goto bail;
return FcInitReinitialize (); ret = FcInitReinitialize ();
bail:
FcConfigDestroy (config);
return ret;
} }
#define __fcinit__ #define __fcinit__

View File

@ -491,11 +491,10 @@ FcFontSetList (FcConfig *config,
{ {
if (!FcInitBringUptoDate ()) if (!FcInitBringUptoDate ())
goto bail0; goto bail0;
}
config = FcConfigGetCurrent (); config = FcConfigReference (config);
if (!config) if (!config)
goto bail0; goto bail0;
}
FcListHashTableInit (&table); FcListHashTableInit (&table);
if (!os) if (!os)
@ -558,7 +557,7 @@ FcFontSetList (FcConfig *config,
*/ */
ret = FcFontSetCreate (); ret = FcFontSetCreate ();
if (!ret) if (!ret)
goto bail0; goto bail1;
for (i = 0; i < FC_LIST_HASH_SIZE; i++) for (i = 0; i < FC_LIST_HASH_SIZE; i++)
while ((bucket = table.buckets[i])) while ((bucket = table.buckets[i]))
{ {
@ -570,6 +569,7 @@ FcFontSetList (FcConfig *config,
if (destroy_os) if (destroy_os)
FcObjectSetDestroy (os); FcObjectSetDestroy (os);
FcConfigDestroy (config);
return ret; return ret;
@ -577,6 +577,7 @@ bail2:
FcFontSetDestroy (ret); FcFontSetDestroy (ret);
bail1: bail1:
FcListHashTableCleanup (&table); FcListHashTableCleanup (&table);
FcConfigDestroy (config);
bail0: bail0:
if (destroy_os) if (destroy_os)
FcObjectSetDestroy (os); FcObjectSetDestroy (os);
@ -588,24 +589,26 @@ FcFontList (FcConfig *config,
FcPattern *p, FcPattern *p,
FcObjectSet *os) FcObjectSet *os)
{ {
FcFontSet *sets[2]; FcFontSet *sets[2], *ret;
int nsets; int nsets;
if (!config) if (!config)
{ {
if (!FcInitBringUptoDate ()) if (!FcInitBringUptoDate ())
return 0; return 0;
config = FcConfigGetCurrent ();
if (!config)
return 0;
} }
config = FcConfigReference (config);
if (!config)
return NULL;
nsets = 0; nsets = 0;
if (config->fonts[FcSetSystem]) if (config->fonts[FcSetSystem])
sets[nsets++] = config->fonts[FcSetSystem]; sets[nsets++] = config->fonts[FcSetSystem];
if (config->fonts[FcSetApplication]) if (config->fonts[FcSetApplication])
sets[nsets++] = config->fonts[FcSetApplication]; sets[nsets++] = config->fonts[FcSetApplication];
return FcFontSetList (config, sets, nsets, p, os); ret = FcFontSetList (config, sets, nsets, p, os);
FcConfigDestroy (config);
return ret;
} }
#define __fclist__ #define __fclist__
#include "fcaliastail.h" #include "fcaliastail.h"

View File

@ -845,7 +845,7 @@ FcFontSetMatch (FcConfig *config,
FcPattern *p, FcPattern *p,
FcResult *result) FcResult *result)
{ {
FcPattern *best; FcPattern *best, *ret = NULL;
assert (sets != NULL); assert (sets != NULL);
assert (p != NULL); assert (p != NULL);
@ -853,17 +853,16 @@ FcFontSetMatch (FcConfig *config,
*result = FcResultNoMatch; *result = FcResultNoMatch;
config = FcConfigReference (config);
if (!config) if (!config)
{ return NULL;
config = FcConfigGetCurrent ();
if (!config)
return 0;
}
best = FcFontSetMatchInternal (sets, nsets, p, result); best = FcFontSetMatchInternal (sets, nsets, p, result);
if (best) if (best)
return FcFontRenderPrepare (config, p, best); ret = FcFontRenderPrepare (config, p, best);
else
return NULL; FcConfigDestroy (config);
return ret;
} }
FcPattern * FcPattern *
@ -873,19 +872,16 @@ FcFontMatch (FcConfig *config,
{ {
FcFontSet *sets[2]; FcFontSet *sets[2];
int nsets; int nsets;
FcPattern *best; FcPattern *best, *ret = NULL;
assert (p != NULL); assert (p != NULL);
assert (result != NULL); assert (result != NULL);
*result = FcResultNoMatch; *result = FcResultNoMatch;
config = FcConfigReference (config);
if (!config) if (!config)
{ return NULL;
config = FcConfigGetCurrent ();
if (!config)
return 0;
}
nsets = 0; nsets = 0;
if (config->fonts[FcSetSystem]) if (config->fonts[FcSetSystem])
sets[nsets++] = config->fonts[FcSetSystem]; sets[nsets++] = config->fonts[FcSetSystem];
@ -894,9 +890,11 @@ FcFontMatch (FcConfig *config,
best = FcFontSetMatchInternal (sets, nsets, p, result); best = FcFontSetMatchInternal (sets, nsets, p, result);
if (best) if (best)
return FcFontRenderPrepare (config, p, best); ret = FcFontRenderPrepare (config, p, best);
else
return NULL; FcConfigDestroy (config);
return ret;
} }
typedef struct _FcSortNode { typedef struct _FcSortNode {
@ -1183,7 +1181,7 @@ FcFontSort (FcConfig *config,
FcCharSet **csp, FcCharSet **csp,
FcResult *result) FcResult *result)
{ {
FcFontSet *sets[2]; FcFontSet *sets[2], *ret;
int nsets; int nsets;
assert (p != NULL); assert (p != NULL);
@ -1191,18 +1189,18 @@ FcFontSort (FcConfig *config,
*result = FcResultNoMatch; *result = FcResultNoMatch;
config = FcConfigReference (config);
if (!config) if (!config)
{ return NULL;
config = FcConfigGetCurrent ();
if (!config)
return 0;
}
nsets = 0; nsets = 0;
if (config->fonts[FcSetSystem]) if (config->fonts[FcSetSystem])
sets[nsets++] = config->fonts[FcSetSystem]; sets[nsets++] = config->fonts[FcSetSystem];
if (config->fonts[FcSetApplication]) if (config->fonts[FcSetApplication])
sets[nsets++] = config->fonts[FcSetApplication]; sets[nsets++] = config->fonts[FcSetApplication];
return FcFontSetSort (config, sets, nsets, p, trim, csp, result); ret = FcFontSetSort (config, sets, nsets, p, trim, csp, result);
FcConfigDestroy (config);
return ret;
} }
#define __fcmatch__ #define __fcmatch__
#include "fcaliastail.h" #include "fcaliastail.h"

View File

@ -42,6 +42,11 @@ test_pthread_LDADD = $(top_builddir)/src/libfontconfig.la
# We don't enable this test by default because it will require config and fonts # We don't enable this test by default because it will require config and fonts
# to meaningfully test anything, and we are not installed yet. # to meaningfully test anything, and we are not installed yet.
#TESTS += test-pthread #TESTS += test-pthread
check_PROGRAMS += test-crbug1004254
test_crbug1004254_LDADD = $(top_builddir)/src/libfontconfig.la
# Disabling this for the same reason as above but trying to run in run-test.sh.
#TESTS += test-crbug1004254
endif endif
check_PROGRAMS += test-bz89617 check_PROGRAMS += test-bz89617
test_bz89617_CFLAGS = \ test_bz89617_CFLAGS = \

View File

@ -20,6 +20,8 @@
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER # DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE. # PERFORMANCE OF THIS SOFTWARE.
set -e
case "$OSTYPE" in case "$OSTYPE" in
msys ) MyPWD=`pwd -W` ;; # On Msys/MinGW, returns a MS Windows style path. msys ) MyPWD=`pwd -W` ;; # On Msys/MinGW, returns a MS Windows style path.
* ) MyPWD=`pwd` ;; # On any other platforms, returns a Unix style path. * ) MyPWD=`pwd` ;; # On any other platforms, returns a Unix style path.
@ -408,4 +410,17 @@ rm -rf $MYCACHEBASEDIR $MYCONFIG my-fonts.conf my-out my-out.expected
fi # if [ "x$EXEEXT" = "x" ] fi # if [ "x$EXEEXT" = "x" ]
if [ -x $BUILDTESTDIR/test-crbug1004254 ]; then
dotest "MT-safe global config"
prep
curl -s -o $FONTDIR/noto.zip https://noto-website-2.storage.googleapis.com/pkgs/NotoSans-hinted.zip
(cd $FONTDIR; unzip noto.zip)
if [ -n ${SOURCE_DATE_EPOCH:-} ] && [ ${#SOURCE_DATE_EPOCH} -gt 0 ]; then
touch -m -t "`date -d \"@${SOURCE_DATE_EPOCH}\" +%y%m%d%H%M.%S`" $FONTDIR
fi
$BUILDTESTDIR/test-crbug1004254
else
echo "No test-crbug1004254: skipped"
fi
rm -rf $FONTDIR $CACHEFILE $CACHEDIR $BASEDIR $FONTCONFIG_FILE out rm -rf $FONTDIR $CACHEFILE $CACHEDIR $BASEDIR $FONTCONFIG_FILE out

116
test/test-crbug1004254.c Normal file
View File

@ -0,0 +1,116 @@
/*
* fontconfig/test/test-pthread.c
*
* Copyright © 2000 Keith Packard
* Copyright © 2013 Raimund Steger
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of the author(s) not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. The authors make no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <fontconfig/fontconfig.h>
struct thr_arg_s
{
int thr_num;
};
static void
run_query (void)
{
FcPattern *pat = FcPatternCreate (), *match;
FcResult result;
FcPatternAddString (pat, FC_FAMILY, "sans-serif");
FcPatternAddBool (pat, FC_SCALABLE, FcTrue);
FcConfigSubstitute (NULL, pat, FcMatchPattern);
FcDefaultSubstitute (pat);
match = FcFontMatch (NULL, pat, &result);
if (result != FcResultMatch || !match)
{
fprintf (stderr, "ERROR: No matches found\n");
}
if (match)
FcPatternDestroy (match);
FcPatternDestroy (pat);
}
static void
run_reinit (void)
{
if (!FcInitReinitialize ())
{
fprintf (stderr, "ERROR: Reinitializing failed\n");
}
}
#define NTEST 3000
static void *
run_test_in_thread (void *arg)
{
struct thr_arg_s *thr_arg = (struct thr_arg_s *) arg;
int thread_num = thr_arg->thr_num;
fprintf (stderr, "Worker %d: started (round %d)\n", thread_num % 2, thread_num / 2);
if ((thread_num % 2) == 0)
{
run_query ();
}
else
{
run_reinit ();
}
fprintf (stderr, "Worker %d: done (round %d)\n", thread_num % 2, thread_num / 2);
return NULL;
}
int
main (int argc, char **argv)
{
pthread_t threads[NTEST];
struct thr_arg_s thr_arg[NTEST];
int i, j;
for (i = 0; i < NTEST; i++)
{
int result;
fprintf (stderr, "Thread %d (worker %d round %d): creating\n", i, i % 2, i / 2);
thr_arg[i].thr_num = i;
result = pthread_create (&threads[i], NULL, run_test_in_thread,
(void *) &thr_arg[i]);
if (result != 0)
{
fprintf (stderr, "Cannot create thread %d\n", i);
break;
}
}
for (j = 0; j < i; j++)
{
pthread_join(threads[j], NULL);
fprintf (stderr, "Joined thread %d\n", j);
}
FcFini ();
return 0;
}