Skip to content

Commit

Permalink
stemmer: Resolve potential issue where stemmer may never be initialized
Browse files Browse the repository at this point in the history
If the initial locale was equal to the current stemming language, we may
never have initialized the stemmer (which could lead to crashes or
stemming being disabled).

So we force the reload to always happen on initialization.
CC: #558
  • Loading branch information
ximion committed Nov 15, 2023
1 parent d0bd9be commit 32182d7
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/as-stemmer.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ G_DEFINE_TYPE (AsStemmer, as_stemmer, G_TYPE_OBJECT)

static gpointer as_stemmer_object = NULL;

static void as_stemmer_reload_internal (AsStemmer *stemmer, const gchar *locale, gboolean force);

/**
* as_stemmer_finalize:
**/
Expand Down Expand Up @@ -76,21 +78,14 @@ as_stemmer_init (AsStemmer *stemmer)

/* we don't use the locale in XML, so it can be POSIX */
locale = as_get_current_locale_posix ();
stemmer->current_lang = as_utils_locale_to_language (locale);

as_stemmer_reload (stemmer, stemmer->current_lang);
/* force a reload for initialization */
as_stemmer_reload_internal (stemmer, locale, TRUE);
#endif
}

/**
* as_stemmer_reload:
* @stemmer: A #AsStemmer
* @locale: The stemming language as POSIX locale.
*
* Allows realoading the #AsStemmer with a different language.
*/
void
as_stemmer_reload (AsStemmer *stemmer, const gchar *locale)
static void
as_stemmer_reload_internal (AsStemmer *stemmer, const gchar *locale, gboolean force)
{
#ifdef HAVE_STEMMING
g_autofree gchar *lang = NULL;
Expand All @@ -99,7 +94,7 @@ as_stemmer_reload (AsStemmer *stemmer, const gchar *locale)
/* check if we need to reload */
lang = as_utils_locale_to_language (locale);
locker = g_mutex_locker_new (&stemmer->mutex);
if (as_str_equal0 (lang, stemmer->current_lang)) {
if (!force && as_str_equal0 (lang, stemmer->current_lang)) {
g_mutex_locker_free (locker);
return;
}
Expand All @@ -119,6 +114,20 @@ as_stemmer_reload (AsStemmer *stemmer, const gchar *locale)
#endif
}

/**
* as_stemmer_reload:
* @stemmer: A #AsStemmer
* @locale: The stemming language as POSIX locale.
*
* Allows realoading the #AsStemmer with a different language.
* Does nothing if the stemmer is already using the selected language.
*/
void
as_stemmer_reload (AsStemmer *stemmer, const gchar *locale)
{
as_stemmer_reload_internal (stemmer, locale, FALSE);
}

/**
* as_stemmer_stem:
* @stemmer: A #AsStemmer
Expand Down

0 comments on commit 32182d7

Please sign in to comment.