[Libosinfo] [libosinfo PATCH 1/3] db: Avoid dereference of null pointer

Fabiano Fidêncio fidencio at redhat.com
Tue May 21 11:35:13 UTC 2019


As any consumer of libosinfo API may pass NULL as the @matched argument
of compare_tree(), the current code could be dereferencing a NULL
pointer when calling `osinfo_tree_set_os()`.

In order to avoid doing so, let's set the os to the OsinfoTree at the
moment the @matched argument is set.

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 osinfo/osinfo_db.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/osinfo/osinfo_db.c b/osinfo/osinfo_db.c
index b7da2b7..c4cd1e4 100644
--- a/osinfo/osinfo_db.c
+++ b/osinfo/osinfo_db.c
@@ -790,6 +790,7 @@ static gboolean compare_tree(OsinfoTree *tree,
         OsinfoTreeList *tree_list = osinfo_os_get_tree_list(os);
         GList *trees = osinfo_list_get_elements(OSINFO_LIST(tree_list));
         GList *tree_iter;
+        gboolean found = FALSE;
 
         for (tree_iter = trees; tree_iter; tree_iter = tree_iter->next) {
             OsinfoTree *os_tree = OSINFO_TREE(tree_iter->data);
@@ -820,8 +821,11 @@ static gboolean compare_tree(OsinfoTree *tree,
                 match_regex(os_treeinfo_version, treeinfo_version) &&
                 match_regex(os_treeinfo_arch, treeinfo_arch)) {
                 *ret_os = os;
-                if (matched != NULL)
+                if (matched != NULL) {
                     *matched = os_tree;
+                    osinfo_tree_set_os(*matched, *ret_os);
+                    found = TRUE;
+                }
                 break;
             }
         }
@@ -829,10 +833,8 @@ static gboolean compare_tree(OsinfoTree *tree,
         g_list_free(trees);
         g_object_unref(tree_list);
 
-        if (*ret_os != NULL) {
-            osinfo_tree_set_os(*matched, *ret_os);
+        if (found)
             return TRUE;
-        }
     }
 
     return FALSE;
-- 
2.21.0




More information about the Libosinfo mailing list