[gnome-software/gnome-42: 1/4] gs-appstream: Add runtime checks for arguments in public API




commit 076cfa22d2f9290fb66eec7a1eeafae1859867bf
Author: Milan Crha <mcrha redhat com>
Date:   Tue Jun 7 12:05:41 2022 +0200

    gs-appstream: Add runtime checks for arguments in public API
    
    While the runtime checks can be disabled in compile time, they
    can help to detect errors in the code.

 lib/gs-appstream.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 67 insertions(+), 3 deletions(-)
---
diff --git a/lib/gs-appstream.c b/lib/gs-appstream.c
index 16a402c33..0dcdcf595 100644
--- a/lib/gs-appstream.c
+++ b/lib/gs-appstream.c
@@ -20,7 +20,13 @@ GsApp *
 gs_appstream_create_app (GsPlugin *plugin, XbSilo *silo, XbNode *component, GError **error)
 {
        GsApp *app;
-       g_autoptr(GsApp) app_new = gs_app_new (NULL);
+       g_autoptr(GsApp) app_new = NULL;
+
+       g_return_val_if_fail (GS_IS_PLUGIN (plugin), NULL);
+       g_return_val_if_fail (XB_IS_SILO (silo), NULL);
+       g_return_val_if_fail (XB_IS_NODE (component), NULL);
+
+       app_new = gs_app_new (NULL);
 
        /* refine enough to get the unique ID */
        if (!gs_appstream_refine_app (plugin, app_new, silo, component,
@@ -967,6 +973,11 @@ gs_appstream_refine_app (GsPlugin *plugin,
        g_autoptr(GPtrArray) launchables = NULL;
        g_autoptr(XbNode) req = NULL;
 
+       g_return_val_if_fail (GS_IS_PLUGIN (plugin), FALSE);
+       g_return_val_if_fail (GS_IS_APP (app), FALSE);
+       g_return_val_if_fail (XB_IS_SILO (silo), FALSE);
+       g_return_val_if_fail (XB_IS_NODE (component), FALSE);
+
        /* is compatible */
        req = xb_node_query_first (component,
                                   "requires/id[@type='id']"
@@ -1411,6 +1422,11 @@ gs_appstream_search (GsPlugin *plugin,
                { AS_SEARCH_TOKEN_MATCH_NONE,   NULL }
        };
 
+       g_return_val_if_fail (GS_IS_PLUGIN (plugin), FALSE);
+       g_return_val_if_fail (XB_IS_SILO (silo), FALSE);
+       g_return_val_if_fail (values != NULL, FALSE);
+       g_return_val_if_fail (GS_IS_APP_LIST (list), FALSE);
+
        /* add some weighted queries */
        for (guint i = 0; queries[i].xpath != NULL; i++) {
                g_autoptr(GError) error_query = NULL;
@@ -1487,6 +1503,11 @@ gs_appstream_add_category_apps (GsPlugin *plugin,
 {
        GPtrArray *desktop_groups;
 
+       g_return_val_if_fail (GS_IS_PLUGIN (plugin), FALSE);
+       g_return_val_if_fail (XB_IS_SILO (silo), FALSE);
+       g_return_val_if_fail (GS_IS_CATEGORY (category), FALSE);
+       g_return_val_if_fail (GS_IS_APP_LIST (list), FALSE);
+
        desktop_groups = gs_category_get_desktop_groups (category);
        if (desktop_groups->len == 0) {
                g_warning ("no desktop_groups for %s", gs_category_get_id (category));
@@ -1577,6 +1598,9 @@ gs_appstream_add_categories (XbSilo *silo,
                             GCancellable *cancellable,
                             GError **error)
 {
+       g_return_val_if_fail (XB_IS_SILO (silo), FALSE);
+       g_return_val_if_fail (list != NULL, FALSE);
+
        for (guint j = 0; j < list->len; j++) {
                GsCategory *parent = GS_CATEGORY (g_ptr_array_index (list, j));
                GPtrArray *children = gs_category_get_children (parent);
@@ -1611,6 +1635,9 @@ gs_appstream_add_popular (XbSilo *silo,
        g_autoptr(GError) error_local = NULL;
        g_autoptr(GPtrArray) array = NULL;
 
+       g_return_val_if_fail (XB_IS_SILO (silo), FALSE);
+       g_return_val_if_fail (GS_IS_APP_LIST (list), FALSE);
+
        /* find out how many packages are in each category */
        array = xb_silo_query (silo,
                               "components/component/kudos/"
@@ -1648,6 +1675,10 @@ gs_appstream_add_recent (GsPlugin *plugin,
        g_autoptr(GError) error_local = NULL;
        g_autoptr(GPtrArray) array = NULL;
 
+       g_return_val_if_fail (GS_IS_PLUGIN (plugin), FALSE);
+       g_return_val_if_fail (XB_IS_SILO (silo), FALSE);
+       g_return_val_if_fail (GS_IS_APP_LIST (list), FALSE);
+
        /* use predicate conditions to the max */
        xpath = g_strdup_printf ("components/component/releases/"
                                 "release[@timestamp>%" G_GUINT64_FORMAT "]/../..",
@@ -1686,6 +1717,10 @@ gs_appstream_add_alternates (XbSilo *silo,
        g_autoptr(GPtrArray) ids = NULL;
        g_autoptr(GString) xpath = g_string_new (NULL);
 
+       g_return_val_if_fail (XB_IS_SILO (silo), FALSE);
+       g_return_val_if_fail (GS_IS_APP (app), FALSE);
+       g_return_val_if_fail (GS_IS_APP_LIST (list), FALSE);
+
        /* probably a package we know nothing about */
        if (gs_app_get_id (app) == NULL)
                return TRUE;
@@ -1743,6 +1778,9 @@ gs_appstream_add_featured (XbSilo *silo,
        g_autoptr(GError) error_local = NULL;
        g_autoptr(GPtrArray) array = NULL;
 
+       g_return_val_if_fail (XB_IS_SILO (silo), FALSE);
+       g_return_val_if_fail (GS_IS_APP_LIST (list), FALSE);
+
        /* find out how many packages are in each category */
        array = xb_silo_query (silo,
                               "components/component/custom/value[@key='GnomeSoftware::FeatureTile']/../..|"
@@ -1782,6 +1820,11 @@ gs_appstream_url_to_app (GsPlugin *plugin,
        g_autofree gchar *xpath = NULL;
        g_autoptr(GPtrArray) components = NULL;
 
+       g_return_val_if_fail (GS_IS_PLUGIN (plugin), FALSE);
+       g_return_val_if_fail (XB_IS_SILO (silo), FALSE);
+       g_return_val_if_fail (GS_IS_APP_LIST (list), FALSE);
+       g_return_val_if_fail (url != NULL, FALSE);
+
        /* not us */
        scheme = gs_utils_get_url_scheme (url);
        if (g_strcmp0 (scheme, "appstream") != 0)
@@ -1812,6 +1855,9 @@ gs_appstream_component_add_keyword (XbBuilderNode *component, const gchar *str)
        g_autoptr(XbBuilderNode) keyword = NULL;
        g_autoptr(XbBuilderNode) keywords = NULL;
 
+       g_return_if_fail (XB_IS_BUILDER_NODE (component));
+       g_return_if_fail (str != NULL);
+
        /* create <keywords> if it does not already exist */
        keywords = xb_builder_node_get_child (component, "keywords", NULL);
        if (keywords == NULL)
@@ -1831,6 +1877,9 @@ gs_appstream_component_add_provide (XbBuilderNode *component, const gchar *str)
        g_autoptr(XbBuilderNode) provide = NULL;
        g_autoptr(XbBuilderNode) provides = NULL;
 
+       g_return_if_fail (XB_IS_BUILDER_NODE (component));
+       g_return_if_fail (str != NULL);
+
        /* create <provides> if it does not already exist */
        provides = xb_builder_node_get_child (component, "provides", NULL);
        if (provides == NULL)
@@ -1850,6 +1899,9 @@ gs_appstream_component_add_category (XbBuilderNode *component, const gchar *str)
        g_autoptr(XbBuilderNode) category = NULL;
        g_autoptr(XbBuilderNode) categories = NULL;
 
+       g_return_if_fail (XB_IS_BUILDER_NODE (component));
+       g_return_if_fail (str != NULL);
+
        /* create <categories> if it does not already exist */
        categories = xb_builder_node_get_child (component, "categories", NULL);
        if (categories == NULL)
@@ -1868,6 +1920,9 @@ gs_appstream_component_add_icon (XbBuilderNode *component, const gchar *str)
 {
        g_autoptr(XbBuilderNode) icon = NULL;
 
+       g_return_if_fail (XB_IS_BUILDER_NODE (component));
+       g_return_if_fail (str != NULL);
+
        /* create <icon>str</icon> if it does not already exist */
        icon = xb_builder_node_get_child (component, "icon", NULL);
        if (icon == NULL) {
@@ -1881,7 +1936,11 @@ gs_appstream_component_add_icon (XbBuilderNode *component, const gchar *str)
 void
 gs_appstream_component_add_extra_info (XbBuilderNode *component)
 {
-       const gchar *kind = xb_builder_node_get_attr (component, "type");
+       const gchar *kind;
+
+       g_return_if_fail (XB_IS_BUILDER_NODE (component));
+
+       kind = xb_builder_node_get_attr (component, "type");
 
        /* add the gnome-software-specific 'Addon' group and ensure they
         * all have an icon set */
@@ -1927,9 +1986,14 @@ gs_appstream_component_add_extra_info (XbBuilderNode *component)
 void
 gs_appstream_component_fix_url (XbBuilderNode *component, const gchar *baseurl)
 {
-       const gchar *text = xb_builder_node_get_text (component);
+       const gchar *text;
        g_autofree gchar *url = NULL;
 
+       g_return_if_fail (XB_IS_BUILDER_NODE (component));
+       g_return_if_fail (baseurl != NULL);
+
+       text = xb_builder_node_get_text (component);
+
        if (text == NULL)
                return;
 


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