[gnome-software/1792-crash-under-gs_flatpak_refine_app_unlocked: 30/30] flatpak: Make sure the XbSilo is non-NULL when passing it to other functions




commit 6f5ddcc1b59baf31cb591cbc7ea480fe52c3fa7f
Author: Milan Crha <mcrha redhat com>
Date:   Tue Jun 7 12:06:59 2022 +0200

    flatpak: Make sure the XbSilo is non-NULL when passing it to other functions
    
    It can happen the GsFlatpak::XbSilo is NULL even after refreshing
    the GsFlatpak, thus make sure the functions properly end with an error,
    rather than expecting the XbSilo is always non-NULL.
    
    Closes https://gitlab.gnome.org/GNOME/gnome-software/-/issues/1792

 plugins/flatpak/gs-flatpak.c | 57 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
---
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index 7880071e9..0090dff31 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -1131,6 +1131,29 @@ gs_flatpak_rescan_app_data (GsFlatpak *self,
        return TRUE;
 }
 
+/* Must be called with a read lock held on @self->silo_lock. */
+static gboolean
+ensure_flatpak_silo_locked (GsFlatpak            *self,
+                            GRWLockReaderLocker **locker,
+                            gboolean              interactive,
+                            GCancellable         *cancellable,
+                            GError              **error)
+{
+       while (self->silo == NULL) {
+               g_clear_pointer (locker, g_rw_lock_reader_locker_free);
+
+               if (!gs_flatpak_rescan_appstream_store (self, interactive, cancellable, error))
+                       return FALSE;
+
+               /* At this point either rescan_appstream_store() returned an error or it successfully
+                * initialised self->silo. There is the possibility that another thread will invalidate
+                * the silo before we regain the lock. If so, we’ll have to rescan again. */
+               *locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       }
+
+       return TRUE;
+}
+
 gboolean
 gs_flatpak_setup (GsFlatpak *self, GCancellable *cancellable, GError **error)
 {
@@ -3198,6 +3221,8 @@ gs_flatpak_refine_app_unlocked (GsFlatpak *self,
                return TRUE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
 
        /* always do AppStream properties */
        if (!gs_flatpak_refine_appstream (self, app, self->silo, flags, interactive, cancellable, error))
@@ -3397,6 +3422,8 @@ gs_flatpak_refine_wildcard (GsFlatpak *self, GsApp *app,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
 
        /* find all apps when matching any prefixes */
        xpath = g_strdup_printf ("components/component/id[text()='%s']/..", id);
@@ -3950,6 +3977,9 @@ gs_flatpak_search (GsFlatpak *self,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
+
        if (!gs_appstream_search (self->plugin, self->silo, values, list_tmp,
                                  cancellable, error))
                return FALSE;
@@ -4024,6 +4054,9 @@ gs_flatpak_search_developer_apps (GsFlatpak *self,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
+
        if (!gs_appstream_search_developer_apps (self->plugin, self->silo, values, list_tmp,
                                                 cancellable, error))
                return FALSE;
@@ -4093,6 +4126,9 @@ gs_flatpak_add_category_apps (GsFlatpak *self,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
+
        return gs_appstream_add_category_apps (self->plugin, self->silo,
                                               category, list,
                                               cancellable, error);
@@ -4111,6 +4147,9 @@ gs_flatpak_add_categories (GsFlatpak *self,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
+
        return gs_appstream_add_categories (self->silo,
                                            list, cancellable, error);
 }
@@ -4129,6 +4168,9 @@ gs_flatpak_add_popular (GsFlatpak *self,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
+
        if (!gs_appstream_add_popular (self->silo, list_tmp,
                                       cancellable, error))
                return FALSE;
@@ -4152,6 +4194,9 @@ gs_flatpak_add_featured (GsFlatpak *self,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
+
        if (!gs_appstream_add_featured (self->silo, list_tmp,
                                        cancellable, error))
                return FALSE;
@@ -4175,6 +4220,9 @@ gs_flatpak_add_deployment_featured (GsFlatpak *self,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
+
        return gs_appstream_add_deployment_featured (self->silo, deployments, list, cancellable, error);
 }
 
@@ -4193,6 +4241,9 @@ gs_flatpak_add_alternates (GsFlatpak *self,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
+
        if (!gs_appstream_add_alternates (self->silo, app, list_tmp,
                                          cancellable, error))
                return FALSE;
@@ -4217,6 +4268,9 @@ gs_flatpak_add_recent (GsFlatpak *self,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
+
        if (!gs_appstream_add_recent (self->plugin, self->silo, list_tmp, age,
                                      cancellable, error))
                return FALSE;
@@ -4242,6 +4296,9 @@ gs_flatpak_url_to_app (GsFlatpak *self,
                return FALSE;
 
        locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_locked (self, &locker, interactive, cancellable, error))
+               return FALSE;
+
        if (!gs_appstream_url_to_app (self->plugin, self->silo, list_tmp, url, cancellable, error))
                return FALSE;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]