Skip to content

Commit

Permalink
Avoid a race condition when loading GResource data
Browse files Browse the repository at this point in the history
  • Loading branch information
ximion committed Sep 14, 2024
1 parent 2a9d29d commit d4fbd56
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
3 changes: 0 additions & 3 deletions compose/asc-compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -2232,9 +2232,6 @@ asc_compose_run (AscCompose *compose, GCancellable *cancellable, GError **error)
temp_dir_created = TRUE;
}

/* sanity check to ensure resources can be loaded */
as_utils_ensure_resources ();

tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) asc_compose_task_free);

for (guint i = 0; i < priv->units->len; i++) {
Expand Down
20 changes: 19 additions & 1 deletion compose/asc-globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ G_DEFINE_TYPE_WITH_PRIVATE (AscGlobals, asc_globals, G_TYPE_OBJECT)
static AscGlobals *g_globals = NULL;
static GMutex g_globals_mutex;

/**
* asc_get_resource_safe:
*
* A threadsafe variant to obtain the buit-in #GResource.
*/
static GResource *
asc_get_resource_safe (void)
{
static GResource *resource = NULL;

if (g_once_init_enter (&resource)) {
GResource *res = asc_get_resource ();
g_once_init_leave (&resource, res);
}

return resource;
}

/**
* asc_compose_error_quark:
*
Expand Down Expand Up @@ -306,7 +324,7 @@ asc_globals_get_pangrams_for (const gchar *lang)
return priv->pangrams_en;

/* load array from resources */
data = g_resource_lookup_data (asc_get_resource (),
data = g_resource_lookup_data (asc_get_resource_safe (),
"/org/freedesktop/appstream-compose/pangrams/en.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
NULL);
Expand Down
2 changes: 0 additions & 2 deletions src/as-utils-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ gchar *as_utils_find_stock_icon_filename_full (const gchar *root_dir,
guint icon_size,
guint icon_scale,
GError **error);
AS_INTERNAL_VISIBLE
void as_utils_ensure_resources (void);

gchar *as_make_usertag_key (const gchar *ns, const gchar *tag);

Expand Down
55 changes: 25 additions & 30 deletions src/as-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@
**/
G_DEFINE_QUARK (as-utils-error-quark, as_utils_error)

/**
* as_get_resource_safe:
*
* A threadsafe variant to obtain our GResource.
*/
static GResource *
as_get_resource_safe (void)
{
static GResource *resource = NULL;

if (g_once_init_enter (&resource)) {
GResource *res = as_get_resource ();
g_once_init_leave (&resource, res);
}

g_assert (resource != NULL);
return resource;
}

/**
* as_markup_strsplit_words:
* @text: the text to split.
Expand Down Expand Up @@ -1259,24 +1278,6 @@ as_utils_search_token_valid (const gchar *token)
return TRUE;
}

/**
* as_utils_ensure_resources:
*
* Perform a sanity check to ensure GResource can be loaded.
*/
void
as_utils_ensure_resources (void)
{
static GMutex mutex;
GResource *resource = NULL;

g_mutex_lock (&mutex);
resource = as_get_resource ();
if (resource == NULL)
g_error ("Failed to load internal resources: as_get_resource() returned NULL!");
g_mutex_unlock (&mutex);
}

/**
* as_utils_is_category_name:
* @category_name: a XDG category name, e.g. "ProjectManagement"
Expand All @@ -1294,8 +1295,7 @@ as_utils_is_category_name (const gchar *category_name)
{
g_autoptr(GBytes) data = NULL;
g_autofree gchar *key = NULL;
GResource *resource = as_get_resource ();
g_assert (resource != NULL);
GResource *resource = as_get_resource_safe ();

/* custom spec-extensions are generally valid if prefixed correctly */
if (g_str_has_prefix (category_name, "X-"))
Expand Down Expand Up @@ -1382,8 +1382,7 @@ as_utils_is_tld (const gchar *tld)
{
g_autoptr(GBytes) data = NULL;
g_autofree gchar *key = NULL;
GResource *resource = as_get_resource ();
g_assert (resource != NULL);
GResource *resource = as_get_resource_safe ();

/* safeguard against accidentally matching comments */
if (as_is_empty (tld) || g_str_has_prefix (tld, "#"))
Expand Down Expand Up @@ -1532,8 +1531,7 @@ as_utils_is_platform_triplet_arch (const gchar *arch)
if (g_str_has_prefix (arch, "#"))
return FALSE;

resource = as_get_resource ();
g_assert (resource != NULL);
resource = as_get_resource_safe ();

/* load the readonly data section */
data = g_resource_lookup_data (resource,
Expand Down Expand Up @@ -1575,8 +1573,7 @@ as_utils_is_platform_triplet_oskernel (const gchar *os)
if (g_str_has_prefix (os, "#"))
return FALSE;

resource = as_get_resource ();
g_assert (resource != NULL);
resource = as_get_resource_safe ();

/* load the readonly data section */
data = g_resource_lookup_data (resource,
Expand Down Expand Up @@ -1618,8 +1615,7 @@ as_utils_is_platform_triplet_osenv (const gchar *env)
if (g_str_has_prefix (env, "#"))
return FALSE;

resource = as_get_resource ();
g_assert (resource != NULL);
resource = as_get_resource_safe ();

/* load the readonly data section */
data = g_resource_lookup_data (resource,
Expand Down Expand Up @@ -1688,8 +1684,7 @@ as_utils_is_reference_registry (const gchar *regname)
if (g_str_has_prefix (regname, "#"))
return FALSE;

resource = as_get_resource ();
g_assert (resource != NULL);
resource = as_get_resource_safe ();

/* load the readonly data section */
data = g_resource_lookup_data (resource,
Expand Down

0 comments on commit d4fbd56

Please sign in to comment.